Introduction
Strings are used to store text, such as names, words, sentences, and special characters.
What Are Strings in Python?
A string is a sequence of characters enclosed within single quotes, double quotes, or triple quotes.
Strings are commonly used to store text data in Python.
Example:
name = "Python"
city = 'Chennai'
message = """Welcome to Python Programming"""
print(name)
print(city)
print(message)
Output:
Python
Chennai
Welcome to Python Programming
How to Create a String in Python?
Python provides three ways to create strings.
1. Single Quotes
We can create a string using single quotes.
name = 'Arun'
print(name)
2. Double Quotes
We can also create a string using double quotes.
name = "Python"
print(name)
3. Triple Quotes
Triple quotes are used to create multiline strings.
message = """Hello,
Welcome to Python.
Let's start learning!"""
print(message)
String Indexing in Python
Indexing is used to access individual characters from a string.
In Python, indexing starts from 0.
There are two types of indexing:
- Positive Indexing: Starts from the beginning of the string (0, 1, 2, 3...).
- Negative Indexing: Starts from the end of the string (-1, -2, -3...).
Example:
name = "Python"
print(name[0])
print(name[1])
print(name[-1])
print(name[-2])
Output:
P
y
n
o
String Slicing in Python
String slicing is used to extract a part of a string.
The syntax is:
string[start:end:step]
-
start: Starting index. -
end: Ending index (not included). -
step: Number of positions to move each time.
Example:
name = "Python Programming"
print(name[0:6])
print(name[7:18])
print(name[:6])
print(name[7:])
Output:
Python
Programming
Python
Programming
String Operations in Python
Python supports different operations on strings.
1. Concatenation
Concatenation is used to combine two or more strings using the + operator.
Example:
first = "Hello"
second = "Python"
result = first + " " + second
print(result)
Output:
Hello Python
2. Repetition
The * operator is used to repeat a string multiple times.
Example:
word = "Python "
print(word * 3)
Output:
Python Python Python
3. Membership
Membership operators are used to check whether a particular character or word exists in a string.
The operators are:
innot in
Example:
text = "Python Programming"
print("Python" in text)
print("Java" not in text)
Output:
True
True
Common String Methods in Python
Python provides several built-in string methods to perform different operations.
| Method | Description |
|---|---|
| upper() | Converts the string into uppercase. |
| lower() | Converts the string into lowercase. |
| capitalize() | Converts the first character to uppercase. |
| title() | Converts the first character of each word to uppercase. |
| strip() | Removes leading and trailing whitespace. |
| replace() | Replaces a specified part of a string. |
| split() | Splits a string into a list. |
| find() | Returns the index of the first occurrence of a substring. |
| count() | Counts the occurrences of a character or substring. |
Example:
text = "hello python"
print(text.upper())
print(text.lower())
print(text.capitalize())
print(text.title())
print(text.replace("python", "world"))
Output:
HELLO PYTHON
hello python
Hello python
Hello Python
hello world
String Immutability in Python
Strings in Python are immutable. This means we cannot directly change individual characters of an existing string.
Example:
name = "Python"
name[0] = "J"
print(name)
Output:
TypeError
If we want to change a string, we need to create a new string.
Correct Example:
name = "Python"
new_name = "J" + name[1:]
print(new_name)
Output:
Jython
Why Are Strings Important in Python?
Strings are important because:
- They help us store and manage text data.
- They are useful for handling names, addresses, and messages.
- They help process and manipulate text.
- They are used in web development and data processing.
- They make it easier to work with user input.
Top comments (0)