String Operations
1. String Creation
- Strings can be created using single, double, or triple quotes for multiline strings.
string1 = 'Hello'
string2 = "World"
string3 = '''This is
a multiline string'''
print(string1)
print(string2)
print(string3)
---------------------------------------
Hello
World
This is
a multiline string
2. Accessing Characters
- Individual characters in a string can be accessed using indexing.
- Negative indexing starts from the end of the string.
string = "Python"
print(string[0]) # Output: P
print(string[-1]) # Output: n
---------------------------------------
P
n
3. String Slicing
- Substrings can be extracted using slicing.
- Use
string[start:end] to get characters from start to end (not inclusive).
string = "Python"
print(string[0:3]) # Output: Pyt
print(string[:3]) # Output: Pyt
print(string[3:]) # Output: hon
---------------------------------------
Pyt
Pyt
hon
4. String Concatenation
- Combine strings using the
+ operator.
string1 = "Hello"
string2 = "World"
result = string1 + " " + string2
print(result) # Output: Hello World
---------------------------------------
Hello World
5. String Multiplication
- Repeat strings using the
* operator.
string = "Hello"
print(string * 3) # Output: HelloHelloHello
---------------------------------------
HelloHelloHello
6. String Length
- Get the length of a string using
len().
string = "Python"
print(len(string)) # Output: 6
---------------------------------------
6
7. Using Loops
- Use a
for loop to iterate through each character in a string.
string = "Python"
for char in string:
print(char)
---------------------------------------
P
y
t
h
o
n
8. Changing Case
- Change the case of strings using methods like
upper(), lower(), title(), etc.
string = "Python Programming"
print(string.upper()) # Output: PYTHON PROGRAMMING
print(string.lower()) # Output: python programming
print(string.title()) # Output: Python Programming
print(string.capitalize()) # Output: Python programming
print(string.swapcase()) # Output: pYTHON pROGRAMMING
---------------------------------------
PYTHON PROGRAMMING
python programming
Python Programming
Python programming
pYTHON pROGRAMMING
9. Stripping Whitespace
- Remove leading, trailing, or both whitespace using
strip(), lstrip(), and rstrip().
string = " Hello World "
print(string.strip()) # Output: Hello World
#Optional
print(string.lstrip()) # Output: Hello World
print(string.rstrip()) # Output: Hello World
---------------------------------------
Hello World
Hello World
Hello World
10. Splitting and Joining Strings
- Use
split() to break a string into parts.
- Use
join() to combine a list of strings into a single string.
string = "Python,Java,C++"
print(string.split(",")) # Output: ['Python', 'Java', 'C++']
words = ["Python", "is", "fun"]
print(" ".join(words)) # Output: Python is fun
---------------------------------------
['Python', 'Java', 'C++']
Python is fun
11. Replacing Substrings
- Use
replace() to replace parts of a string with another string.
string = "I like Java"
print(string.replace("Java", "Python")) # Output: I like Python
---------------------------------------
I like Python
12. Finding Substrings
- Find the position of a substring using
find(). Returns -1 if not found.
string = "Python Programming"
print(string.find("Programming")) # Output: 7
print(string.find("Java")) # Output: -1
---------------------------------------
7
-1
13. String Formatting
- Format strings using
f-strings or the format() method.
name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
print("My name is {} and I am {} years old.".format(name, age))
---------------------------------------
My name is Alice and I am 25 years old.
My name is Alice and I am 25 years old.
14. String Validation
- Check properties of strings using methods like
isalpha(), isdigit(), isalnum(), etc.
string = "Python123"
print(string.isalpha()) # Output: False
print(string.isdigit()) # Output: False
print(string.isalnum()) # Output: True
---------------------------------------
False
False
True
15. Reversing a String
- Reverse a string using slicing
[::-1].
string = "Python"
print(string[::-1]) # Output: nohtyP
---------------------------------------
nohtyP