A string is a sequence of characters used to store text in Python. Strings can contain letters, numbers, spaces, and special characters.
A string is a collection or sequence of characters. For example:
name = "Rahul"
print(name)
Here, "Rahul" is a string.
Python allows you to create strings using single or double quotation marks.
name = 'Rahul'
city = "Patna"
print(name)
print(city)
A string can be enclosed in single quotation marks.
message = 'Hello Python'
print(message)
A string can also be enclosed in double quotation marks.
message = "Hello Python"
print(message)
Both single and double quotes can be used to create normal strings.
Triple quotes are commonly used for multi-line strings.
message = """Hello
Welcome to
Python"""
print(message)
A string can contain numbers, but they are treated as characters.
code = "12345"
print(code)
print(type(code))
The len() function returns the number of characters in a string.
name = "Rahul"
print(len(name))
Spaces are also counted as characters.
text = "Hello World"
print(len(text))
Each character in a string has a position called an index. Python starts indexing from 0.
name = "Python"
print(name[0])
print(name[1])
print(name[2])
Python also supports negative indexing. The last character has index -1.
name = "Python"
print(name[-1])
print(name[-2])
print(name[-3])
String slicing is used to extract a part of a string.
name = "Python"
print(name[0:3])
The ending index is not included.
text = "Programming"
print(text[:4])
When the starting index is omitted, Python starts from the beginning.
text = "Programming"
print(text[4:])
When the ending index is omitted, Python continues to the end.
Concatenation means joining two or more strings. The + operator is used for concatenation.
first = "Hello"
second = "Python"
result = first + " " + second
print(result)
The * operator can be used to repeat a string.
text = "Python "
print(text * 3)
The in operator can be used to check whether a character or substring exists in a string.
text = "Python"
print("P" in text)
print("z" in text)
The not in operator checks whether a value does not exist inside a string.
text = "Python"
print("Java" not in text)
print("Python" not in text)
Python provides methods such as upper() and lower() for changing the case of a string.
text = "Python Programming"
print(text.upper())
print(text.lower())
Strings in Python are immutable. This means that individual characters of an existing string cannot be changed directly.
text = "Python"
# text[0] = "J"
The above operation is not allowed because strings cannot be modified character by character.
Instead, create a new string:
text = "Python"
text = "J" + text[1:]
print(text)
F-strings are a convenient way to insert variables into strings.
name = "Rahul"
age = 20
print(f"My name is {name} and I am {age} years old.")
Escape characters are used to represent special characters inside strings.
| Escape Character | Meaning |
|---|---|
\n |
New line |
\t |
Tab |
\\ |
Backslash |
\" |
Double quotation mark |
\' |
Single quotation mark |
print("Hello\nWorld")
print("Python\tProgramming")
Strings can be compared using comparison operators.
a = "apple"
b = "banana"
print(a == b)
print(a != b)
first_name = "Rahul"
last_name = "Kumar"
full_name = first_name + " " + last_name
print("Full Name:", full_name)
print("Length:", len(full_name))
print("Uppercase:", full_name.upper())
print("Lowercase:", full_name.lower())
Question: What is the index of the first character of a Python string?