Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
string slicing
slicing
string
communitycreator

What is Python slicing?

Saad Akbar

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

Overview

Python slicing is used to return a range of characters from a string. This is done by specifying a start and an end index separated by a colon. The first character will have a 0 index value.

Code example

x="Educative"
print(x[3:6])
Python slicing

Explanation

This prints the string starting from the 3rd index until the 6th index.

Slice from the start

The range will start from the first index up to the index specified by the user.

Example

x="Educative"
print(x[:6])
Slicing from the start

Explanation

This prints the string from the start until the 6th index.

Slice to the end

In this case, we'll specify a start index, and the end index will be left blank so that a string from the specified start to the end index is printed.

Example

x="Educative"
print(x[3:])
Slice to the end

Explanation

This prints the string from the 3rd to the last index.

Negative indexing

This is used to begin the slicing from the end of a string.

Example

x="Educative"
print(x[-8:-4])
Negative indexing

Explanation

This prints the string from the -8 to -4 index values, which in this case are from d until a.

RELATED TAGS

python
string slicing
slicing
string
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring