Eddie Kim
0
Q:

python string index of

string = "Hello World"
string_slice = string[1:10] # "ello Worl" (index 1 up to 10)
string_last_chars = string[-5:] # "World" (5th index from last to the end)
H = string[0] # "H"
6
s = "mouse"
animal_letter = s.find('s')
print animal_letter
3
str = 'codegrepper'
# str[start:end:step]
#by default: start = 0, end = len(str), step = 1
print(str[:]) 		#codegrepper
print(str[::]) 		#codegrepper
print(str[5:]) 		#repper
print(str[:8]) 		#codegrep
print(str[::2]) 	#cdgepr
print(str[2:8]) 	#degrep
print(str[2:8:2]) 	#dge
#step < 0 : reverse
print(str[::-1]) 	#reppergedoc
print(str[::-3]) 	#rpgo
# str[start:end:-1]	means start from the end, go backward and stop at start
print(str[8:3:-1]) 	#pperg
4
sentence = 'Python programming is fun.'

result = sentence.index('is fun')
print("Substring 'is fun':", result)

result = sentence.index('Java')
print("Substring 'Java':", result)
0
str.index(sub[, start[, end]] )
-1

New to Communities?

Join the community