String Slices code example
Example 1: 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
Example 2: python slice string
name = "dx4iot"
print(name[0:]) #output: dx4iot
print(name[0:3]) #output: dx4
#==== reverse a string ====#
print(name[::-1]) #output: toi4xd
print(name[0:6:2]) #output: d4o
print(name[-4:-1]) #output: 4io
Example 3: 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
Example 4: javascript slice string from character
var input = 'john smith~123 Street~Apt 4~New York~NY~12345';
var fields = input.split('~');
var name = fields[0];
var street = fields[1];
Example 5: String Slices
s = 'Hello'
# 01234 ## Showing the index numbers for the 'Hello'
s[1:4] ## 'ell' -- starting at 1, up to but not including 4
s[0:2] ## 'He'