Lessons

Python Basics

Python Variables

Operators in Python

Conditional Statements in Python

Python Lists

Python Tuples

Python Sets

Python Dictionaries

Loops in Python

Python Arrays and Functions

Conclusion

Python Modify Strings

Modify Strings in Python

Python has several built-in methods for modifying strings:

  • Uppercase: Convert the string to uppercase:
python
1
2
a = "Hello, World!"
print(a.upper())  # Output: 'HELLO, WORLD!'
  • Lowercase: Convert the string to lowercase:
python
1
print(a.lower())  # Output: 'hello, world!'
  • Remove Whitespace: Use strip() to remove spaces from the beginning and end:
python
1
2
a = " Hello, World! "
print(a.strip())  # Output: 'Hello, World!'
  • Replace String: Replace characters or substrings using replace():
python
1
print(a.replace("H", "J"))  # Output: 'Jello, World!'
  • Split String: Use split() to split a string into a list based on a separator:
python
1
print(a.split(","))  # Output: ['Hello', ' World!']

Frequently Asked Questions