Python Tutorial - String Case Conversion

In Python, to facilitate case conversion of letters in strings, string variables provide three functions:

  • title functions
  • lower functions
  • upper functions

Python Title Function

The title function is used to convert the first letter of each word in the string to uppercase, and all other letters to lowercase. After the conversion is complete, this method returns the converted string. If there are no characters in the string that need to be converted, this method returns the string intact.

The syntax of the title function is as follows:

str.title()

Where str represents the string to be converted.

[Example:]

#convert first letter of each word in the string to uppercase
string = "www.freelearningpoints.com"
string.title()
#no character in the string that need to be converted
string = "NICE TO MEET YOU"
string.title()

The output is:

'Www.Freelearningpoints.Com'
'Nice To Meet You'

Python Lower Function

The lower function is used to convert all uppercase letters in the string to lowercase letters. After the conversion is completed, the method returns the newly obtained string. If the string is already lowercase, the method returns the original string.

The syntax of the lower function is as follows:

str.lower()

Where str represents the string to be converted.

[Example:]

string = “NICE TO MEET YOU”
string.lower()

The output is:

'nice to meet you'

Python Upper Function

The function of upper() is exactly the opposite of the lower(). It is used to convert all lowercase letters in the string to uppercase letters. The return method is the same as the above two methods, that is, if the conversion is successful, a new string is returned; Otherwise, the original string is returned.

The syntax of the upper function is as follows:

str.upper()

Where str represents the string to be converted.

[Example:]

string = "nice to meet you"
# convert all lowercase letters in the string to uppercase letters
string.upper()

The output is:

'NICE TO MEET YOU'

                               


More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression


More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises