Strings in Python
Topic: Perform String Manipulations Using Built- in
Methods
Objective:
To explore and apply Python’s built- in string methods to manipulate and process text
effectively.
1. Basic String Operations
text = " Hello Python "
print([Link]()) # Remove spaces from both sides
print([Link]()) # Convert to lowercase
print([Link]()) # Convert to uppercase
print([Link]()) # Capitalize each word
print([Link]("Python", "World")) # Replace word
Output:
Hello Python
hello python
HELLO PYTHON
Hello Python
Hello World
2. String Slicing and Indexing
word = "Programming"
print(word[0]) # First character
print(word[- 1]) # Last character
print(word[0:6]) # Slice substring
print(word[::- 1]) # Reverse string
Output:
P
g
Progra
gnimmargorP
3. String Search Methods
sentence = "Python is amazing"
print(sentence.find("is")) # Find substring
print([Link]("Py")) # Check start
print([Link]("ing")) # Check end
Output:
7
True
True
4. String Validation Methods
s1 = "Hello123"
s2 = "123"
s3 = "Hello"
print([Link]()) # Alphanumeric
print([Link]()) # Digits only
print([Link]()) # Alphabets only
Output:
True
True
True
5. Splitting and Joining Strings
msg = "Python is fun"
words = [Link]() # Split into list
print(words)
joined = "- ".join(words) # Join with hyphen
print(joined)
Output:
['Python', 'is', 'fun']
Python- is- fun
6. Counting and Replacing
text = "banana"
print([Link]("a")) # Count occurrences
print([Link]("a", "o")) # Replace character
Output:
3
bonono
7. String Formatting
name = "Aditya"
age = 21
print(f"My name is {name} and I am {age} years old.")
Output:
My name is Aditya and I am 21 years old.
8. Case Conversion Methods
txt = "pyTHon iS fUn"
print([Link]()) # First letter capital
print([Link]()) # Swap cases
print([Link]()) # Title case
Output:
Python is fun
PYthON Is FuN
Python Is Fun
Conclusion:
String methods make text processing in Python simple and efficient.
They allow tasks like searching, formatting, validation, and transformation to be
done easily using built- in functions.