Python String Cases Conversion Tutorial
In this tutorial, you will discover how to perform string case conversions in Python. If you are new to Python programming and seeking a reference to expand your knowledge and skills, this tutorial provides insights into fundamental operations commonly employed in Python applications. Sample code snippets are included, along with explanations for better understanding.
What Is String Case Conversion?
String case conversion is a process that transforms the letter case of a given string into specific other cases. For example, imagine you have a string written in lowercase, and you need to convert it to uppercase for particular project requirements.
This type of operation is also frequently employed in other programming languages, such as PHP and JavaScript. Here is a list of additional languages that discuss string conversion within their specific programming contexts:
String cases conversions includes the following:
How to Convert String Cases in Python
Python provides several valuable built-in methods for string manipulation. Some of these include upper(), lower(), and capitalize(), which are useful for the purposes of this tutorial.
How to Convert a String to Uppercase in Python
To convert a string to uppercase, we will use Python's upper() built-in method. The upper() method is designed to change a given string from lowercase to uppercase. Here's a sample code snippet that demonstrates how to use this method:
- # Sample lower case String
- lstring = "sourcecodester.com free source code and tutorials"
- # Convert string to Upper Case
- lstring = lstring.upper()
- # Output
- print(lstring)
How to Convert the First Letter of a String to Uppercase in Python
In Python, we can convert the first letter of a string to uppercase using the built-in method called capitalize(). The capitalize() method is designed to capitalize the initial letter of a given string, which aligns with our objective. Refer to the code snippet below for a demonstration of how to use this method:
- # Sample lower case String
- lstring = "sourcecodester.com free source code and tutorials"
- # Convert string's first letter to Uppercase
- cstring = lstring.capitalize()
- # Output
- print(cstring)
How to Convert a String to Lowercase in Python
In Python, you can convert a string to lowercase using the lower() built-in method. This method is specifically designed to transform all uppercase characters in the given string into lowercase. The code snippet below provides a clear example of how to utilize this method:
- # Sample lower case String
- string = "sourcecodester.com Free Source Codes and Tutorials"
- # Convert string's first letter to Uppercase
- lstring = string.lower()
- # Output
- print(lstring)
How to Capitalize the First Letter of Each Word in a String in Python
In certain situations, we may need to capitalize the first letter of each word in a given string to fulfill specific project requirements. To achieve this, we can iterate through the characters of the string to identify which letters to capitalize based on the conditions we set. We can establish a condition that checks whether the character is the first letter of a word. We can use the isspace() method to determine if the previous character was a space, indicating that the current character in the loop is the first letter of a word. Here's a code snippet that illustrates this process:
- # Sample string
- string = "sourcecodester.com free source codes and tutorials"
- # Convert each words of the string first letter to Upper Case
- i = 0
- isSpaceBefore = False
- convertedString = ""
- for l in string:
- # Check if character is first or has space before
- if (i == 0 or isSpaceBefore == True) and not (l.isspace()):
- convertedString += l.upper()
- else:
- convertedString += l
- isSpaceBefore = l.isspace()
- i += 1
- # Output
- print(convertedString)
Summary
You can easily convert string cases in Python using the useful built-in methods called upper(), lower(), and capitalize(). These methods are primarily designed to convert string cases accordingly.
There you have it! I hope this String Cases Conversion Tutorial in Python will assist you with your current or future projects. Explore more on this website for additional Free Source Codes, Tutorials, and Articles covering various programming languages.
Happy Coding =)
Add new comment
- 58 views