Python change case string

How to Change Python String Cases

Dealing with strings in Python is common. One popular operation you may want to perform on a string is to change the case to upper or lower case.

  1. To convert a Python string to lowercase, use the built-in lower() method of a string.
  2. To convert a Python string to uppercase, use the built-in upper() method.
"Hello, world".upper() # HELLO WORLD "HELLO, WORLD".lower() # hello world

Let’s see other case conversions in Python.

How to Check If a String Is in Lowercase/Uppercase

You may find it useful to be able to check if a string is already in the lower or upper case. Unsurprisingly, there are built-in methods for doing this in Python.

To test if a string is in uppercase or lowercase in Python, use the built-in isupper() and islower() methods:

"Hello, world".islower() # False "HELLO, WORLD".isupper() # True

How to Capitalize the First Letter of a String in Python

Sometimes you might only want to change the case of the first letter of a string. In this case, you don’t want to convert the entire string to upper case. Because this is such a frequent task to do, there is also a built-in method in Python for capitalizing the first letter of a word.

To capitalize the first letter of a string in Python, use the built-in capitalize() method.

"hello, world".capitalize() # Hello, world

How to Swap Cases in Python

A less frequent operation to perform on a string is to convert lower case letters to upper case and vice versa. If you’re facing this situation, there is a useful built-in function you can use.

To convert each lowercase letter to uppercase and vice versa, use the swapcase() method.

"HELLO, world".swapcase() # hello, WORLD

How to Title Case a String in Python

The title case refers to a string where the first letter of each word is capitalized.

To capitalize the first letter of each word in a string, use the title case converter by calling the title() method.

"hello, world".title() # Hello, World

Conclusion

Today you learned how to convert strings to lowercase and to uppercase in Python. Also, you saw some examples of how to apply other casings too.

Читайте также:  What is padding in html table

Make sure you check out other useful string methods in Python.

Thanks for reading. I hope you find it useful.

Further Reading

Источник

Tech Tutorials

Tutorials and posts about Java, Spring, Hadoop and many more. Java code examples and interview questions. Spring code examples.

Saturday, February 15, 2020

Changing String Case in Python

In this tutorial we’ll go through all the methods which are used for changing string case in Python.

In Python there are various methods for changing case of String and also for checking if String is lower case, upper case etc.

Summary of the methods for changing String case in Python

  1. str.lower()— Return a copy of the string with all the cased characters converted to lowercase.
  2. str.upper()- Return a copy of the string with all the cased characters converted to uppercase.
  3. str.capitalize()— Return a copy of the string with its first character capitalized and the rest lowercased.
  4. str.title()— Return a titlecased version of the string where words start with an uppercase character and the remaining characters are lowercase.

There there are also methods to check the String case, these methods can be used to verify if changing case is really required or not.

  1. str.islower()— Return true if all cased characters in the string are lowercase and there is at least one cased character, false otherwise.
  2. str.isupper()- Return true if all cased characters in the string are uppercase and there is at least one cased character, false otherwise.
  3. str.istitle()— Return true if the string is a titlecased string and there is at least one character.

Changing String case in Python examples

1. Changing String to all lower case or to all upper case.

s = "This is a TEST String" print('String in all lower case-',s.lower()) s = "This is a Test String" print('String in all upper case-', s.upper())
String in all lower case- this is a test string String in all upper case- THIS IS A TEST STRING

2. Capitalizing the String. First character will be capitalized, if there is any other upper case character in the String that is lower cased.

s = "this is a TEST String" print('String Capitalized-',s.capitalize())
String Capitalized- This is a test string

3. String title cased. Using title() method you can title cased a String. This method capitalizes the first character of every word.

s = "this is a TEST String" print('String Title cased-',s.title())
String Title cased- This Is A Test String

4. Checking the case before changing. You can also check the case before changing the case of the String and change the case only if needed. This is an optimization because any String modification method results in a creation of a new String as String is immutable in Python.

s = "this is a test string" #change only if not already in lower case if not s.islower(): s = s.lower() else: print('String already in lower case') print(s)
String already in lower case this is a test string
s = "This is a test String" #change only if not already in upper case if not s.isupper(): s = s.upper() else: print('String already in upper case') print(s)

That’s all for this topic Changing String Case in Python. If you have any doubt or any suggestions to make please drop a comment. Thanks!

Читайте также:  Пример редирект 301 php

Источник

Tech Tutorials

Tutorials and posts about Java, Spring, Hadoop and many more. Java code examples and interview questions. Spring code examples.

Saturday, February 15, 2020

Changing String Case in Python

In this tutorial we’ll go through all the methods which are used for changing string case in Python.

In Python there are various methods for changing case of String and also for checking if String is lower case, upper case etc.

Summary of the methods for changing String case in Python

  1. str.lower()— Return a copy of the string with all the cased characters converted to lowercase.
  2. str.upper()- Return a copy of the string with all the cased characters converted to uppercase.
  3. str.capitalize()— Return a copy of the string with its first character capitalized and the rest lowercased.
  4. str.title()— Return a titlecased version of the string where words start with an uppercase character and the remaining characters are lowercase.

There there are also methods to check the String case, these methods can be used to verify if changing case is really required or not.

  1. str.islower()— Return true if all cased characters in the string are lowercase and there is at least one cased character, false otherwise.
  2. str.isupper()- Return true if all cased characters in the string are uppercase and there is at least one cased character, false otherwise.
  3. str.istitle()— Return true if the string is a titlecased string and there is at least one character.

Changing String case in Python examples

1. Changing String to all lower case or to all upper case.

s = "This is a TEST String" print('String in all lower case-',s.lower()) s = "This is a Test String" print('String in all upper case-', s.upper())
String in all lower case- this is a test string String in all upper case- THIS IS A TEST STRING

2. Capitalizing the String. First character will be capitalized, if there is any other upper case character in the String that is lower cased.

s = "this is a TEST String" print('String Capitalized-',s.capitalize())
String Capitalized- This is a test string

3. String title cased. Using title() method you can title cased a String. This method capitalizes the first character of every word.

s = "this is a TEST String" print('String Title cased-',s.title())
String Title cased- This Is A Test String

4. Checking the case before changing. You can also check the case before changing the case of the String and change the case only if needed. This is an optimization because any String modification method results in a creation of a new String as String is immutable in Python.

s = "this is a test string" #change only if not already in lower case if not s.islower(): s = s.lower() else: print('String already in lower case') print(s)
String already in lower case this is a test string
s = "This is a test String" #change only if not already in upper case if not s.isupper(): s = s.upper() else: print('String already in upper case') print(s)

That’s all for this topic Changing String Case in Python. If you have any doubt or any suggestions to make please drop a comment. Thanks!

Читайте также:  Games with codes in java

Источник

Оцените статью