Extract am is z from the string my name is z from python3

Asked 2 years ago, Updated 2 years ago, 176 views

I want to extract am is z from the string my name is z from python3[4: ] Not like this Extract from a, and if z comes out, it's over. [a: z] For example, if it's adkzed, I want to print only adkz Please reply. Thank you!!

slice python3 indexing

2022-09-22 19:41

2 Answers

See the example below.

In [11]: s = 'sdkzedz1'
In [12]: s[:s.rindex('z')]
Out[12]: 'sdkzed'
In [13]: s[:s.index('z')]
Out[13]: 'sdk'


2022-09-22 19:41

String can only be extracted with numbers

To extract only adkz from adkzed, you need to write down the position of a and the position of z. Only int can fit in the location, nothing else.

s = "adkzed"

s[0:3] >> "adkz" is output

s[a:z] You can't do this. Here we're trying to find the variable value of a and the variable value of z, but these are undefined.

s = "my name is z"

s[4:] >> "ame is z" output


2022-09-22 19:41

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.