Strings in Python
Strings in Python
In Python, a string is a sequence of characters. Strings are enclosed in single (') or double
(") quotes. You can perform various operations on strings like concatenation, slicing, and
traversal. Strings in Python are immutable, meaning their content cannot be changed once
created.
1. String Operations
a. Concatenation:
Example:
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result) # Output: "Hello World"
b. Repetition:
Example:
str = "Python "
result = str * 3
print(result) # Output: "Python Python Python "
c. Membership:
You can check if a substring exists within a string using in or not in.
Example:
str = "Hello, World!"
print("Hello" in str) # Output: True
print("Hi" not in str) # Output: True
d. Slicing:
Example:
str = "Hello, World!"
print(str[0:5]) # Output: "Hello"
print(str[7:]) # Output: "World!"
print(str[:5]) # Output: "Hello"
print(str[-6:]) # Output: "World!"
Example:
str = "Hello"
for char in str:
print(char)
Output:
H
e
l
l
o
a. len():
Example:
str = "Hello"
print(len(str)) # Output: 5
b. capitalize():
Example:
str = "hello"
print(str.capitalize()) # Output: "Hello"
c. title():
Example:
str = "hello world"
print(str.title()) # Output: "Hello World"
d. lower():
Example:
str = "HELLO"
print(str.lower()) # Output: "hello"
e. upper():
Example:
str = "hello"
print(str.upper()) # Output: "HELLO"
f. count():
Example:
str = "hello world, hello everyone"
print(str.count("hello")) # Output: 2
g. find():
Returns the index of the first occurrence of a substring. Returns -1 if not found.
Example:
str = "hello world"
print(str.find("world")) # Output: 6
print(str.find("python")) # Output: -1
h. index():
Example:
str = "hello world"
print(str.index("world")) # Output: 6
# print(str.index("python")) # Raises ValueError
i. endswith():
Checks if the string ends with a specified substring. Returns True or False.
Example:
str = "hello world"
print(str.endswith("world")) # Output: True
j. startswith():
Checks if the string starts with a specified substring. Returns True or False.
Example:
str = "hello world"
print(str.startswith("hello")) # Output: True
k. isalnum():
Returns True if all characters in the string are alphanumeric (letters or digits).
Example:
str = "Hello123"
print(str.isalnum()) # Output: True
l. isalpha():
Example:
str = "Hello"
print(str.isalpha()) # Output: True
m. isdigit():
Example:
str = "12345"
print(str.isdigit()) # Output: True
n. islower():
Example:
str = "hello"
print(str.islower()) # Output: True
o. isupper():
Example:
str = "HELLO"
print(str.isupper()) # Output: True
p. isspace():
Example:
str = " "
print(str.isspace()) # Output: True
r. replace():
Example:
str = "Hello World"
print(str.replace("World", "Python")) # Output: "Hello Python"
s. join():
Joins the elements of an iterable (list, tuple) into a string, with a separator.
Example:
words = ["Hello", "World"]
print(" ".join(words)) # Output: "Hello World"
t. partition():
Splits the string into three parts: the part before the separator, the separator itself, and the part
after the separator.
Example:
str = "Hello World"
print(str.partition(" ")) # Output: ('Hello', ' ', 'World')
u. split():
Example:
str = "Hello World"
print(str.split()) # Output: ['Hello', 'World']