Full-stack Python development: python string slicing,
String Slicing
String slices are a type of string operators.
The Slice operation is to extract the desired substring from the string.
Syntax format: str [start, end, step]
Start position
End position (not included)
Step (1 by default)
Common Operations
[:] Extract the entire string
[Start:] extract start to end
[: End] extract the start to end (not included)
[Start: end] extract start to end (not included)
[Start: end: step] extract one character for each length from start to end (not included)
1 str = 'abcdefg' 2 print (str [:]) 3 print (str [2:]) 4 print (str [: 4]) 5 print (str [2: 4]) 6 print (str []) 7 print (str [-3:]) # extract the last three digits of 8 print (str [-1:]) # extract the last 9 print (str [:-1]) # string Inversion
Result:
Refer:
Http://www.runoob.com/python3/python3-string.html