When I started learning Python, one of the first things I learned was how to work with strings.
Strings are used in almost every program, whether we are displaying messages, collecting user input, or working with text.
In this blog, let's understand Python strings with simple examples.
What is a string in Python?
A string is a sequence of characters enclosed in single quotes, double quotes, or triple quotes.
language = "Python"
message = 'Hello World'
print(language)
print(message)
Output:
Python
Hello World
1. Accessing characters using indexing
Each character in a string has an index. Python indexing starts from 0, which means the first character is at index 0.
language = "Python"
print(language[0])
print(language[1])
print(language[5])
Output:
P
y
n
Python also supports negative indexing, which starts from the end of the string.
language = "Python"
print(language[-1])
print(language[-2])
Output:
n
o
Negative indexing is useful when we want to access characters from the end without knowing the exact length of the string.
2. String slicing
Slicing allows us to extract a part of a string.
The syntax is:
string[start:stop:step]
-
startspecifies where slicing begins. -
stopspecifies where slicing ends, but the stop index is not included. -
stepspecifies how many positions to move each time.
Example:
language = "Python"
print(language[0:3])
print(language[2:5])
print(language[::2])
Output:
Pyt
tho
Pto
We can also reverse a string using slicing.
language = "Python"
print(language[::-1])
Output:
nohtyP
Here, the step value is -1, so Python moves backward through the string.
3. Useful string methods
Python provides many built-in methods to work with strings.
text = " hello python "
print(text.upper())
print(text.lower())
print(text.strip())
print(text.replace("python", "world"))
Output:
HELLO PYTHON
hello python
hello python
hello world
Some useful string methods are:
-
upper()– Converts characters to uppercase. -
lower()– Converts characters to lowercase. -
strip()– Removes whitespace from both ends. -
replace()– Replaces part of a string. -
split()– Splits a string into a list. -
find()– Finds the position of a substring.
4. Strings are immutable
One important concept in Python is that strings are immutable.
This means we cannot change an individual character in an existing string.
language = "Python"
language[0] = "J"
This code produces a TypeError.
Instead, we can create a new string:
language = "Python"
language = "J" + language[1:]
print(language)
Output:
Jython
We did not modify the original string. We created a new string and assigned it to the variable.
5. String concatenation
We can combine two or more strings using the + operator.
first_word = "Hello"
second_word = "World"
message = first_word + " " + second_word
print(message)
Output:
Hello World
We can also use f-strings to insert variables into a string.
language = "Python"
level = "Beginner"
print(f"I am learning {language} at a {level} level.")
Output:
I am learning Python at a Beginner level.
F-strings make it easier to create readable messages using variables.
Conclusion
Python strings are simple to understand, but they are also very powerful.
Learning indexing, slicing, string methods, and immutability helps us work with text more confidently.
Top comments (0)