Strings in Python with Examples
In Python, strings are sequences of characters used to represent text. Strings can be created using single quotes ('
), double quotes ("
), or triple quotes ('''
or """
) for multi-line text. Strings are among the most commonly used data types in Python, and they come with a variety of built-in methods for manipulation.
How to Create Strings in Python
# Using single quotes
string1 = 'Hello, Python!'
print(string1)
# Using double quotes
string2 = "Python is fun!"
print(string2)
# Using triple quotes for multi-line strings
string3 = '''This is
a multi-line
string.'''
print(string3)
Output:
Hello, Python!
Python is fun!
This is
a multi-line
string.
1. String Indexing and Slicing
Strings in Python are indexed, starting from 0 for the first character. You can access individual characters or extract a substring using slicing.
Example: Indexing and Slicing
text = "Python Programming"
# Accessing individual characters
print("First character:", text[0])
print("Last character:", text[-1])
# Slicing the string
print("First six characters:", text[:6])
print("Characters from index 7 to 17:", text[7:18])
Output:
First character: P
Last character: g
First six characters: Python
Characters from index 7 to 17: Programming
2. Common String Methods
Python provides several built-in methods to manipulate strings.
Example: String Methods
text = " Python is Awesome! "
# Convert to lowercase
print("Lowercase:", text.lower())
# Remove whitespace from the beginning and end
print("Stripped:", text.strip())
# Replace a substring
print("Replace 'Python' with 'Coding':", text.replace("Python", "Coding"))
# Split the string into a list
print("Split:", text.split())
Output:
Lowercase: python is awesome!
Stripped: Python is Awesome!
Replace 'Python' with 'Coding': Coding is Awesome!
Split: ['Python', 'is', 'Awesome!']
3. String Concatenation and Repetition
You can join strings using the +
operator and repeat them using the *
operator.
Example: Concatenation and Repetition
first_name = "Dharmender"
last_name = "Chauhan"
# String concatenation
full_name = first_name + " " + last_name
print("Full Name:", full_name)
# String repetition
repeated_text = "Hello! " * 3
print("Repeated Text:", repeated_text)
Output:
Full Name: Dharmender Chauhan
Repeated Text: Hello! Hello! Hello!
4. f-Strings (String Interpolation)
Python’s f-strings (formatted string literals) provide an easy way to include variables inside strings.
Example: Using f-Strings
name = "Amit"
age = 25
# Using f-string
greeting = f"My name is {name} and I am {age} years old."
print(greeting)
Output:
My name is Amit and I am 25 years old.
5. Checking Substrings
You can check if a substring exists within a string using the in
keyword.
Example: Checking Substrings
text = "Python programming is fun"
# Check for substring
print("Does 'Python' exist in text?", "Python" in text)
print("Does 'Java' exist in text?", "Java" in text)
Output:
Does 'Python' exist in text? True
Does 'Java' exist in text? False
Python strings are versatile and easy to work with. Whether you’re slicing, concatenating, or formatting, mastering strings will help you write cleaner and more efficient code.
Keep Learning 🙂