0% found this document useful (0 votes)
2 views3 pages

Week 2 - Python LAQ Assignment

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)
2 views3 pages

Week 2 - Python LAQ Assignment

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 Manipulation in Python

Introduction
String manipulation refers to the process of creating, modifying, and analyzing
strings in a programming language. In Python, a string is a sequence of
characters enclosed in single quotes (' '), double quotes (" "), or triple quotes ('''
''' or """ """). Python provides a wide range of built-in methods to perform
operations on strings efficiently.

Importance of String Manipulation


String manipulation plays a crucial role in programming for the following reasons:
1. Data Processing: Helps in cleaning and organizing raw data.
2. User Input Handling: Processes and validates input provided by users.
3. Text Formatting: Improves readability of output.
4. Search and Validation: Used for pattern matching (e.g., emails,
passwords).
5. File Handling: Essential for reading and writing text files.

Key Feature
Strings in Python are immutable, which means their values cannot be changed
after creation. Any operation on a string returns a new string.

Common String Methods with Examples


1. Case Conversion Methods
s = "hello world"

print([Link]()) # Output: HELLO WORLD


print([Link]()) # Output: hello world
print([Link]()) # Output: Hello World
print([Link]()) # Output: Hello world

2. Whitespace Removal
s = " Python "

print([Link]()) # Output: "Python"


print([Link]()) # Output: "Python "
print([Link]()) # Output: " Python"
3. Splitting Strings
s = "apple,banana,grape"

print([Link](","))
# Output: ['apple', 'banana', 'grape']

4. Joining Strings
items = ['apple', 'banana', 'grape']

print(", ".join(items))
# Output: apple, banana, grape

5. Replacing Substrings
s = "I like Java"

print([Link]("Java", "Python"))
# Output: I like Python

6. Finding and Searching


s = "hello world"

print([Link]("world")) # Output: 6
print([Link]("he")) # Output: True
print([Link]("ld")) # Output: True

7. Checking String Content


s1 = "Python"
s2 = "12345"

print([Link]()) # Output: True


print([Link]()) # Output: True
print([Link]()) # Output: True

8. Counting and Length


s = "banana"

print([Link]("a")) # Output: 3
print(len(s)) # Output: 6
9. String Slicing
s = "Python"

print(s[0:3]) # Output: Pyt


print(s[-1]) # Output: n
print(s[::-1]) # Output: nohtyP

10. String Formatting


name = "John"
age = 25

print(f"My name is {name} and I am {age} years old.")


# Output: My name is John and I am 25 years old.

Conclusion
String manipulation is an essential aspect of Python programming. It enables
developers to handle and process text data efficiently. With the help of various
built-in methods, Python makes string operations simple, powerful, and flexible,
which is useful in real-world applications like data processing, web development,
and automation.

You might also like