python string slicing

Solutions on MaxInterview for python string slicing by the best coders in the world

showing results for - "python string slicing"
Valentino
27 May 2017
1string = "something"
2
3slice = string[0:3] # will be "som"
4slice = string[0:-3] # will be "someth"
5slice = string[3:] # will be "thing"
6slice = string[:3] # same as first slice
7slice = string[::2] # will be "smtig" -- it goes 2 step each time
Simon
20 Jan 2018
1name = "dx4iot"
2print(name[0:]) #output: dx4iot
3print(name[0:3]) #output: dx4
4#==== reverse a string ====#
5print(name[::-1]) #output: toi4xd
6print(name[0:6:2]) #output: d4o
7print(name[-4:-1]) #output: 4io
8
Anissa
14 Sep 2017
1str = 'codegrepper'
2# str[start:end:step]
3#by default: start = 0, end = len(str), step = 1
4print(str[:]) 		#codegrepper
5print(str[::]) 		#codegrepper
6print(str[5:]) 		#repper
7print(str[:8]) 		#codegrep
8print(str[::2]) 	#cdgepr
9print(str[2:8]) 	#degrep
10print(str[2:8:2]) 	#dge
11#step < 0 : reverse
12print(str[::-1]) 	#reppergedoc
13print(str[::-3]) 	#rpgo
14# str[start:end:-1]	means start from the end, go backward and stop at start
15print(str[8:3:-1]) 	#pperg
Amelia
22 Apr 2017
1my_string = "Hey, This is a sample text"
2print(my_string[2:]) #prints y, This is a sample text
3print(my_string[2:7]) #prints y, Th excluding the last index
4print(my_string[2::2]) #prints y hsi  apetx
5print(my_string[::-1]) #reverses the string => txet elpmas a si sihT ,yeH
queries leading to this page
python string slicing in detailstring index pythonhow slice string pythonslicing in python stringpython string sliceslice a string in pythonslicing string in pythonpython index stringstring slice python from middle to endslice string in pythonindex string pythonhow to slice string pythonhow to slice python stringhow to slice string in pythonpython slice stringslicing of string in pythonindexing string pythonhow to slice a string in pythonindex of string pythonpython string indexingindexing a string pythonslice string pythonstring slicingslice a string using pythonpython string indexpython program to index stringpython how to slice a stringpython slice a stringstring slice pyhtonstring slice pythonslice a string pythonpython slice character from stringslicing a string pythonstring indexing and slicing in pythonslicing a string in pythonget string after index pythonpython indexing stringsslice start of string pythonstring slicing pythonindexing a string in pythonstring method python slicingpython str slicepython string slice examplespyhon slice full stringstring index pythonstring index in pythonstring slicing in pythonpytohn string character rangeextracting index range in string pythonslicing strings pythonhow to use string index in pythonstring indexes pythonindex python stringstr slice pythonpython str indexingpython index of stringstring indexing pythonstring sliceng programthird caracter of a stringstring slicing syntax in pythonpython get range of characters in stringpython string slicingexamples of string slicing in pythonstring indexing in pythonhow to index a string in pythonpython string slicing