dk09
dk09

Reputation: 87

python - index a split string

I am attempting to .index() a string after I split it with .split(). I am setting the split as a variable in order to use it in the index command, but I am not getting an output if I try and print the index. Here is a bit of the code I am working on:

x_1 = y.split(var_1, 1)[1]
start1 = x_1.index(var_2)
print start1

x_1 is the string after the split, with y being the initial string I need to split. var_1 is the location I want to split y at and var_2 is where in x_1 I want to index at. So, start1 is the index location I want to locate within the split string.

I do this but print does not show up in the output. Any help is appreciated. Thanks

Upvotes: 2

Views: 15170

Answers (1)

scotty3785
scotty3785

Reputation: 7006

I suspect you want to use a different syntax instead of split. This is called slicing. Here is a simple example

x = 'this is a string'
start_index = 4
end_index = 12
out = x[start_index:end_index]
print(out)

This will give you the output

' is a st'

This is called 'slicing' a string.

If you just want to get a single character from a string you can use

one_char = x[7]

to get the 7th character from x.

Upvotes: 3

Related Questions