Languages
[Edit]
EN

Python - string slicing

0 points
Created by:
Meredith-Soto
325

In this article, we would like to show you how to use string slicing in Python.

Syntax

Edit

By using the following slice syntax you can return a range of characters:

or

Where:

  • start_index -  index position, from where it will start fetching the characters (default value is 0),
  • end_index -  index position, where it will end fetching the characters (default value is end of the string),
  • step - interval between each character (default value is 1)

Note:

is equivalent to:

Positive indexing

Edit

In this example, we specify the start index and the end index to return a part of the string using positive indexing only.

Output:

Negative indexing

Edit

In this example, we specify the start index and the end index to return a part of the string using negative indexing only.

Output:

Slice from the start

Edit

In this example, we don't specify the start index to start slicing from the index 0 by default.

Output:

Slice to the end

Edit

In this example, we don't specify the end index to end slicing at the end of the string by default.

Output:

Using different steps

Edit

1. Positive indexing

Edit

In this example, we specify the step to iterate e.g every second value from the start of the string.

Output:

2. Negative indexing

Edit

In this example, we specify the step to iterate e.g every second value from the end of the string.

Output:

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Python - string (popular problems)

Python - string slicing
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join