0% found this document useful (0 votes)
4 views2 pages

Python String and List Techniques

The document provides examples of string and list manipulation in Python. It covers various string operations such as reversing, converting to uppercase, replacing substrings, splitting, and checking prefixes. Additionally, it demonstrates list operations including appending, removing elements, accessing by index, sorting, and using list comprehension to create a list of squares.

Uploaded by

Samarpan Kayal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Python String and List Techniques

The document provides examples of string and list manipulation in Python. It covers various string operations such as reversing, converting to uppercase, replacing substrings, splitting, and checking prefixes. Additionally, it demonstrates list operations including appending, removing elements, accessing by index, sorting, and using list comprehension to create a list of squares.

Uploaded by

Samarpan Kayal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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)

You might also like