bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths

Loading lesson path

Learn/Python/Foundations
Python•Foundations

Python - Slicing Strings

Slicing

You can return a range of characters by using the slice syntax.

Specify the start index and the end index, separated by a colon, to return a part of the string.

Example

b = "Hello, World!"

print(b[2:5])

Note

The first character has index 0.

Slice From the Start

By leaving out the start index, the range will start at the first character:

Example

b = "Hello, World!"

print(b[:5])

Slice To the End

By leaving out the end index, the range will go to the end:

Example

b = "Hello, World!"

print(b[2:])

Negative Indexing

Example

b = "Hello, World!"

print(b[-5:-2])

Previous

Python Strings

Next

Python - Modify Strings