Python String and String Methods


Strings in Python

Strings are one of the most commonly used data types in Python, used to handle text data. A string in Python is a sequence of characters enclosed in either single, double, or triple quotes.



1. Assign String to a Variable in Python

Example:

greeting = "Hello, World!"
print(greeting)

Output:

Hello, World!

SEO Tip: This is how you declare a string in Python using double quotes.



2. Quotes Inside Quotes in Python String

Python supports multiple ways to include quotes inside a string:


A. Using Double Quotes Outside and Single Inside:

message = "It's a sunny day"
print(message)

Output:

It's a sunny day

B. Using Single Quotes Outside and Double Inside:

quote = 'She said, "Hello!"'
print(quote)

Output:

She said, "Hello!"


C. Escaping Quotes Using Backslash:

text = "He said, \"Let's go!\""
print(text)

Output:

He said, "Let's go!"


D. Using Triple Quotes (Multi-line or Mix of Quotes):

dialogue = """He said, "It's Pythonic!" """
print(dialogue)

Output:

He said, "It's Pythonic!"

Use case: Best when the string contains both single and double quotes.



3. String Indexing in Python

Strings are indexed in Python. Each character has a position (index), starting from 0 (left to right) and -1 (right to left).

Example:

name = "Python"
print(name[0])   # First character
print(name[3])   # Fourth character

Output:

P
h


Negative Indexing Example:

word = "Learning"
print(word[-1])  # Last character
print(word[-3])  # Third-last character

Output:

g
n

String Slicing in Python

String slicing in Python allows you to extract a subset of characters from a string. It's one of the most important operations when working with strings. Understanding how to use Python string slicing effectively will help you manipulate and extract data from strings with ease.

What is String Slicing in Python?

String slicing allows you to access parts of a string by specifying a start index, an end index, and an optional step. The general syntax is:

string[start:end:step]

start: The index where the slice starts (inclusive).

end: The index where the slice ends (exclusive).

step: The interval between each character (optional).



Basic Slicing - Example 1

You can slice a string by specifying only the start and end indices.

text = "Python Programming"
sliced_text = text[0:6]  # From index 0 to 5 (not including index 6)
print(sliced_text)

Output:

Python

Here, the slice text[0:6] extracts the first 6 characters from the string "Python Programming", starting from index 0 and ending before index 6.



Omitting the Start or End Index - Example 2

If you omit the start index, the slice starts from the beginning of the string.

If you omit the end index, the slice continues until the end of the string.


Example 1: Omitting Start Index

text = "Python Programming"
sliced_text = text[:6]  # From the start to index 5
print(sliced_text)

Output:

Python

Example 2: Omitting End Index

text = "Python Programming"
sliced_text = text[7:]  # From index 7 to the end
print(sliced_text)

Output:

Programming


Using Step in String Slicing - Example 3

The step value allows you to specify a stride between characters. A positive step moves from left to right, and a negative step moves from right to left.

Example 1: Positive Step


text = "Python Programming"
sliced_text = text[0:14:2]  # Every 2nd character from index 0 to 13
print(sliced_text)

Output:

Pto rgamn

In this example, the slice text[0:14:2] extracts every second character from index 0 to index 13, giving us "Pto rgamn".

Example 2: Negative Step (Reverse Slicing)

text = "Python Programming"
sliced_text = text[14:0:-1]  # Reverse from index 14 to 1
print(sliced_text)

Output:

gnimmargorP

Here, text[14:0:-1] extracts the characters from index 14 (the last character) to index 1 in reverse order.



String Slicing with Negative Indices - Example 4

You can use negative indices in slicing to count from the end of the string. Python allows negative indexing where -1 refers to the last character, -2 to the second last, and so on.

text = "Python Programming"
sliced_text = text[-10:-4]  # From index -10 to -4 (not including -4)
print(sliced_text)

Output:

ogram

In this case, text[-10:-4] extracts the characters from the 10th character from the end up to (but not including) the 4th character from the end.

Slicing with Step and Negative Indices - Example 5


You can combine step with negative indices to slice strings in reverse order.

text = "Python Programming"
sliced_text = text[-1:-15:-2]  # Reverse slice with a step of 2
print(sliced_text)

Output:

gnmr

In this example, text[-1:-15:-2] starts from the last character and slices the string in reverse, taking every second character.

Examples of Different String Slicing Techniques



Example 1: Extracting First 5 Characters

text = "Python Programming"
print(text[:5])  # First 5 characters

Output:

Pytho

Example 2: Extracting Last 5 Characters

text = "Python Programming"
print(text[-5:])  # Last 5 characters

Output:

ming

Example 3: Reverse the Entire String

text = "Python"
print(text[::-1])  # Reverse the string

Output:

nohtyP