Introduction to Python Strings
Python Strings
A string is a sequence of characters enclosed in single quotes ('
) or double quotes ("
).
Strings are one of the fundamental data types in Python and are used to represent textual data.
Here are some examples of strings:
name = 'Alice'
message = "Hello, World!"
address = "123 Main Street"
Strings in Python are immutable, meaning that once created, they cannot be modified. However, you can perform various operations and manipulations on strings using built-in string methods and operators.
Here are some common operations and examples:
String Concatenation:
You can concatenate two or more strings together using the +
operator.
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print(full_name)
String Length:
You can obtain the length of a string using the len()
function.
message = "Hello, World!"
length = len(message)
print(length)
String Indexing:
You can access individual characters in a string using indexing, where the first character has an index of 0
.
message = "Hello"
print(message[0])
print(message[3])
String Slicing:
You can extract a portion of a string using slicing, which allows you to specify a range of indices.
message = "Hello, World!"
print(message[0:5])
print(message[7:])
String Methods:
Python provides a variety of built-in string methods for manipulating and working with strings.
Some common methods include upper()
, lower()
, strip()
, split()
, replace()
, and more.
Here are a few examples:
message = "Hello, World!"
print(message.upper())
print(message.lower())
print(message.strip())
print(message.split(","))
print(message.replace("Hello", "Hi"))
String Formatting:
Python provides multiple ways to format strings, allowing you to combine variables or values within a string. Some common formatting methods include f-strings, str.format()
, and %
operator.
name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
print("My name is {} and I am {} years old.".format(name, age))
print("My name is %s and I am %d years old." % (name, age))
String Methods for Manipulation:
Python offers a variety of string methods for manipulating and modifying strings. Some examples include startswith()
, endswith()
, count()
, find()
, isalpha()
, isdigit()
, join()
, and more.
message = "Hello, World!"
print(message.startswith("Hello"))
print(message.endswith("World!"))
print(message.count("l"))
print(message.find("World"))
print(message.isalpha())
print(message.isdigit())
String Escaping:
If you need to include special characters within a string, you can use escape sequences. Common escape sequences include \n
for a new line, \t
for a tab, \"
for a double quote, and \'
for a single quote.
message = "Hello\nWorld!"
print(message)
quote = "He said, \"Hello!\""
print(quote)
String Concatenation with Join:
If you have a list of strings that you want to concatenate, you can use the join()
method to join them with a specific delimiter.
fruits = ["apple", "banana", "orange"]
combined_fruits = ", ".join(fruits)
print(combined_fruits)