Hello and welcome, in this post we will explore all the methods of Python strings that are used for case conversion in Python. We will see how to use them with one example of each.
1. lower()
Converts every character of the string to lowercase.
my_str = "A STRING"
my_str = my_str.lower()
print(my_str)
Output:
a string
2. upper()
Converts every character of the string to uppercase.
my_str = "a string"
my_str = my_str.upper()
print(my_str)
Output:
A STRING
3. title()
Converts the first character of each word to upper case.
my_str = "a string"
my_str = my_str.title()
print(my_str)
Output:
A String
4. capitalize()
Converts the first character to the upper case.
my_str = "a string"
my_str = my_str.capitalize()
print(my_str)
Output:
A string
5. swapcase()
Swaps cases, the lower case becomes the upper case and vice versa.
my_lower_str = "a string"
my_upper_str = "A STRING"
my_str_1 = my_lower_str.swapcase()
my_str_2 = my_upper_str.swapcase()
print(my_str_1)
print(my_str_2)
Output:
A STRING
a string
6. center()
Returns a centered string.
my_str = "a string"
my_str = my_str.center(20, "-")
print(my_str)
Output:
------a string------
7. casefold()
Converts string into lowercase.
my_str = "a sTRINg"
my_str = my_str.casefold()
print(my_str)
Output:
a string
Conclusion
We hope this article on Case conversion in python assignment expert will help you to learn how to use case conversion methods of a string in Python to convert a string from one case to another.
Thank you for visiting our website.
Also Read:
- Python | Check Armstrong Number using for loop
- Python | Factorial of a number using for loop
- Create your own ChatGPT with Python
- Radha Krishna using Python Turtle
- Python Programming Examples | Fundamental Programs in Python
- Python | Delete object of a class
- Python | Modify properties of objects
- Python classmethod
- Python | Create a class that takes 2 parameters, print both parameters
- Python | Create a class, create an object of the class, access, and print property value
- Python | Find the maximum element in a list using lambda and reduce()
- Python | filter numbers(that are not divisible by 3 and 4) from a list using lambda and filter()
- Python | lambda function that returns the maximum of 2 numbers
- Python | Convert all strings of a list to uppercase using lambda
- Python | Square numbers of a list using lambda
- Python | Reverse and convert a string to uppercase using lambda
- Python | Convert String to uppercase using lambda
- Python | Reverse a list using lambda
- Python | Calculator using lambda
- Python | Square of a number using lambda
- Python | Multiply numbers using lambda
- Python | lambda with None
- Python | lambda with pass statement
- Python | Add numbers using lambda
- Python | Print Namaste using lambda
- Iterate over a string in Python
- Python | join() | Join list of strings
- Python | isalnum() method | check if a string consists only of alphabetical characters or numerical digits
- Python | isupper() and islower() methods to check if a string is in all uppercase or lowercase letters
- Python | count substring in a String | count() method