Case conversion in Python

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:

Share:

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at admin@copyassignment.com Thank you