String and List Manipulation in Python
String Manipulation Examples
# 1. Reverse a string
text = "Hello, World!"
reversed_text = text[::-1]
print("Reversed String:", reversed_text)
# 2. Convert a string to uppercase
uppercase_text = [Link]()
print("Uppercase String:", uppercase_text)
# 3. Replace a substring
replaced_text = [Link]("World", "Python")
print("Replaced String:", replaced_text)
# 4. Split a string into a list of words
words = [Link]()
print("Split Words:", words)
# 5. Check if a string starts with a specific substring
starts_with_hello = [Link]("Hello")
print("Starts with 'Hello'?", starts_with_hello)
List Manipulation Examples
# 1. Create a list and append an element
fruits = ["apple", "banana", "cherry"]
[Link]("orange")
print("List after append:", fruits)
# 2. Remove an element from the list
[Link]("banana")
print("List after removal:", fruits)
# 3. Access list elements by index
first_fruit = fruits[0]
print("First Fruit:", first_fruit)
# 4. Sort the list
[Link]()
print("Sorted List:", fruits)
# 5. List comprehension to create a list of squares
numbers = [1, 2, 3, 4, 5]
squares = [x ** 2 for x in numbers]
print("Squares:", squares)