xxxxxxxxxx
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
xxxxxxxxxx
string = "something"
slice = string[0:3] # will be "som"
slice = string[0:-3] # will be "someth"
slice = string[3:] # will be "thing"
slice = string[:3] # same as first slice
slice = string[::2] # will be "smtig" -- it goes 2 step each time
xxxxxxxxxx
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
xxxxxxxxxx
# The first number starts counting from '0'
# Always remember that second number in the square bracket says
# "up to but not including"
s = 'Monty Python'
print(s[0:4]) # Output: Mont
print(s[6:7]) # Output: P
print(s[6:20]) # It doesn't give a Traceback eventhough there are no 19 letters
# Output: Python
# If don't demarcate the first or second number it will go till the start or end accordingly
print(s[:2]) # Output: Mo
print(s[8:]) # Output: thon
print(s[:]) # Output: Monty Python
xxxxxxxxxx
varible = "c#,python,java,javascript,rudy,css,html"
print(varible[0:2])# c#
print(varible[3:9])#python
print(varible[10:14])#java
print(varible[:2])#same as c#
xxxxxxxxxx
>>> x = "Follow us on Softhunt.net"
>>> x[0:6]
'Follow'
>>> x[-18:-4]
'us on Softhunt'
xxxxxxxxxx
# Python program to demonstrate
# string slicing
# String slicing
String ='ASTRING'
# Using indexing sequence
print(String[:3])
print(String[1:5:2])
print(String[-1:-12:-2])
# Prints string in reverse
print("\nReverse String")
print(String[::-1])
#AST
#SR
#GITA
#Reverse String
#GNIRTSA
xxxxxxxxxx
arr[start:stop] # items start through stop-1
arr[start:] # items start through the rest of the array
arr[:stop] # items from the beginning through stop-1
arr[:] # a copy of the whole array
arr[start:stop:step] # start through not past stop, by step
xxxxxxxxxx
# Python program to demonstrate
# string slicing
# String slicing
String = 'GEEKSFORGEEKS'
# Using indexing sequence
print(String[1:12:-2])