Skip to main content

Python - Slicing Strings

Slicing

The slice syntax can be utilized to obtain a selection of characters within a range.

To obtain a specific portion of a string, indicate the beginning index and the ending index separated by a colon using the slice notation.

Example

Retrieve the characters starting from index 3 up to but not including index 7:

x = "Python slicing"

print(x[3:7])


Note: The first character has index 0.

Slice From the Start

If you don't specify the starting index, the range will start from the first character:

Example

Retrieve the first 5 characters of the string, excluding the character at the 6th position:

x = "Python slicing"

print(x[:6])


Slice To the End

If you exclude the end index when defining a range, it will automatically include all the elements until the end:

Example

Retrieve all the characters from the 3rd position to the end of the string:

x = "Python slicing"

print(x[3:])


Negative Indexing

To begin the slice from the end of the string, utilize negative indexes:

Example

Get the characters:

From: "l" in "slicing" (position -6)

To, but not included: "n" in "slicing" (position -2):

x = "Python slicing"

print(x[-6:-2])

Comments

Labels

Show more

Popular post

Top 10 greatest goalkeeper of all time, theirs name and why they are greatest

Who are the top 10 greatest goalkeepers of all time? In the history of football, there have been many great goalkeepers who have contributed to their team's success and won numerous trophies. The role of a goalkeeper in football is crucial, as they are the last line of defense and can often be the main source of motivation and the difference between winning and losing a trophy. So, questions can be arise who is the best goalkeeper of this competition or top 10 greatest goalkeepers of all time. What are some defining characteristics or skills of the greatest goalkeepers on this list? Shot-stopping ability: One of the key attributes of a great goalkeeper is their ability to make crucial saves and prevent goals. The top goalkeepers are known for their lightning-fast reflexes and agility, allowing them to make acrobatic saves and keep the ball out of the net. Command of the penalty area: Great goalkeepers are also known for their ability to command their penalty area and communicate...

Artificial intelligence and its impact on society.

Introduction Artificial intelligence (AI) is a rapidly growing field that has the potential to revolutionize society in ways we never imagined. It is a branch of computer science that focuses on the development of intelligent machines that can perform tasks that typically require human intervention. The impact of AI on society has been a topic of debate for many years. While some people see AI as a way to improve our lives, others worry that it will lead to job loss and other negative consequences. In this article, we will explore the impact of AI on society. Positive Impact of AI on Society 1. Improved Healthcare: AI has the potential to transform healthcare by providing better diagnosis, personalized treatment plans, and early detection of diseases. With the help of AI, doctors can analyze patient data and medical records to identify potential health problems before they become serious. 2. Increased Safety: AI can be used to monitor and identify potential security threats, which ca...

What is email validation?

Email validation is an interaction that checks whether a particular email address is deliverable. We do not want to send email to anyone who does not have an active email. Sometimes we make typos so the validation engine checks if that particular email domain is reliable like Gmail and Yahoo. Validation engine helps maintain your delivery rate up to 99%. How does it work? 1. SPF SPF allows the collector to browse whether an email claiming to be from a particular domain came from an IP address approved by that domain's administrators. Typically, a domain administrator will authorize the IP addresses used by their own outbound MTAs, including any intermediaries or shining shops. 2. DKIM First, DKIM verifies the content of the message by establishing a digital signature. Second, using digital certificates, signature verification keys are distributed through DNS. Thus, a message is associated with a domain name. 3. DMARC DMARC allows the specification of a policy for message authentica...