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:

  1. # Sample lower case String
  2. lstring = "sourcecodester.com free source code and tutorials"
  3.  
  4. # Convert string to Upper Case
  5. lstring = lstring.upper()
  6.  
  7. # Output
  8. print(lstring)

How to Convert string to Uppercases in Python

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:

  1. # Sample lower case String
  2. lstring = "sourcecodester.com free source code and tutorials"
  3.  
  4. # Convert string's first letter to Uppercase
  5. cstring = lstring.capitalize()
  6.  
  7. # Output
  8. print(cstring)

How to Convert string First Letter to Upper Case in Python

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:

  1. # Sample lower case String
  2. string = "sourcecodester.com Free Source Codes and Tutorials"
  3.  
  4. # Convert string's first letter to Uppercase
  5. lstring = string.lower()
  6.  
  7. # Output
  8. print(lstring)

How to Convert string First Letter to Upper Case in Python

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:

  1. # Sample string
  2. string = "sourcecodester.com free source codes and tutorials"
  3.  
  4. # Convert each words of the string first letter to Upper Case
  5. i = 0
  6. isSpaceBefore = False
  7. convertedString = ""
  8. for l in string:
  9.     # Check if character is first or has space before
  10.     if (i == 0 or isSpaceBefore == True) and not (l.isspace()):
  11.         convertedString += l.upper()
  12.     else:
  13.         convertedString += l
  14.     isSpaceBefore = l.isspace()
  15.     i += 1
  16.  
  17. # Output
  18. print(convertedString)

How to Convert the First Letters each words in string to Upper Case in Python

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