python3 return a list of indexes of a specific character in a string
string='mississippi'
s='s'
lst= []
for i in range(len(string)):
if (string[i] == s):
lst.append(i)
print(lst)
#result: [2, 3, 5, 6]
python3 return a list of indexes of a specific character in a string
string='mississippi'
s='s'
lst= []
for i in range(len(string)):
if (string[i] == s):
lst.append(i)
print(lst)
#result: [2, 3, 5, 6]
python string indexing
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
python string slicing
my_string = "Hey, This is a sample text"
print(my_string[2:]) #prints y, This is a sample text
print(my_string[2:7]) #prints y, Th excluding the last index
print(my_string[2::2]) #prints y hsi apetx
print(my_string[::-1]) #reverses the string => txet elpmas a si sihT ,yeH
python string index
# Start from zero and the return value is exact index of first letter
python string index of
sentence = 'Python programming is fun.'
result = sentence.index('is fun')
print("Substring 'is fun':", result)
result = sentence.index('Java')
print("Substring 'Java':", result)
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us