Python:Substring

From Progzoo
Jump to: navigation, search

Python uses a "slice" notation for retrieving a substring from a string. It is written like this string[a:b].

[5:7] extracts characters (takes a slice) from position 5 to just before position 7.

Note that this can also be used to get a slice from array like structures.

0 1 2 3 4 5 6 7 8 9 10 11 12
O n e t w o t h r e e
f o u r f i v e s i x



[Font] [Default] [Show] [Resize] [History] [Profile]

Use [a:] to get part of a string.

[5:] extracts characters form position 5 to the end. The : is required to show that you want to go to the end of the string.

0 1 2 3 4 5 6 7 8 9 10 11 12
O n e t w o t h r e e
f o u r f i v e s i x



[Font] [Default] [Show] [Resize] [History] [Profile]

Use [a] to get a single character from a string.

[5] extracts the character form position 5.

0 1 2 3 4 5 6 7 8 9 10 11 12
O n e t w o t h r e e
f o u r f i v e s i x



[Font] [Default] [Show] [Resize] [History] [Profile]