Python String

In Python, a string is a sequence of characters. These characters can be letters, digits, symbols, and even whitespace characters such as space and tab. Strings are immutable, which means that once they are created, they cannot be changed. If you need to modify a string, you will have to create a new one. There are two ways to create a string in Python: by enclosing characters in single quotes (‘) or double quotes (“). For example:

string1 = "Hello, world!"
string2 = 'Hello, world!'

You can use either single quotes or double quotes to create a string, but you need to be consistent in your choice. For example, if you start a string with a single quote, you must end it with a single quote. Strings can be concatenated or joined together using the + operator. For example:

string1 = "Hello"
string2 = "world"

greeting = string1 + ", " + string2 + "!"
print(greeting)

Output:

Hello, world!

You can also use the * operator to repeat a string a certain number of times. For example:

string1 = "Hello"

greeting = string1 * 3
print(greeting)

Output:

HelloHelloHello

In Python, you can access individual characters in a string using indexing. The first character in a string has an index of 0, the second character has an index of 1, and so on. For example:

string1 = "Hello, world!"

first_char = string1[0]
second_char = string1[1]

print(first_char)
print(second_char)

Output:

H
e

You can also use negative indexing to access characters in a string, with the last character having an index of -1, the second-to-last character having an index of -2, and so on. For example:

string1 = "Hello, world!"

last_char = string1[-1]
second_to_last_char = string1[-2]

print(last_char)
print(second_to_last_char)

Output:

!
d

In addition to indexing, you can also use slicing to extract a part of a string. Slicing is done using the colon (:) operator. For example:

string1 = "Hello, world!"

substring = string1[2:5]

print(substring)

Output:

llo

There are many useful methods available for strings in Python, such as lower(), upper(), strip(), replace(), and split(). These methods allow you to perform common string operations without having to write your own code. For example:

string1 = "   Hello, world!   "

# Remove leading and trailing whitespace
string2 = string1.strip()

# Convert

Table of Contents

String formatting

In Python, string formatting allows you to include variables within a string. Here are a few examples of string formatting in Python:

# Using the % operator
name = "John"
print("Hello, %s!" % name)  # Output: "Hello, John!"

# Using the .format() method
age = 30
print("{} is {} years old.".format(name, age))  # Output: "John is 30 years old."

# Using f-strings (available in Python 3.6+)
print(f"{name} is {age} years old.")  # Output: "John is 30 years old."

You can also specify a width and precision when formatting numbers. For example:

# Formatting a float to 2 decimal points
price = 12.34678
print("Price: $%.2f" % price)  # Output: "Price: $12.35"

# Padding a string to a certain width
name = "John"
print("|%10s|" % name)  # Output: "|      John|"
Was this helpful?
[0]