
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Lowercase Letters to Uppercase in Python
Converting the lowercase letters in a string to uppercase is a common task in Python. When working with the user input or formatting output, Python provides efficient ways to handle the letter casing.
The most commonly used method is the built-in string method upper(). We can use loops and list comprehensions with this method, if we need to convert individual tokens of a string to upper case.
Using python upper() Method
The Python upper() method is used to convert all the lowercase characters present in a string into uppercase. However, if there are elements in the string that are already in uppercase, this method will skip those elements.
Syntax
Following is the syntax of Python upper() method ?
str.upper()
Example 1
Let's look at the following example, where we are going to consider the basic usage of the upper() method.
x = "tutorialspoint" y = x.upper() print(y)
Output
Output of the above program is as follows ?
TUTORIALSPOINT
Example 2
Consider another scenario where we are going to use the islower() method and manually loop through each character and convert it only if it is lowercase.
x = "hello welcome" y = "" for a in x: if a.islower(): y += a.upper() else: y += a print("Result :", y)
Output
The output of the above program is as follows ?
Result : HELLO WELCOME
Python List Comprehension
The python List comprehension offers a simple way to create a list using a single line of code. It combines loops and conditional statements, making it efficient compared to the traditional methods of list construction.
Syntax
Following is the syntax of Python list comprehension ?
newlist = [expression for item in iterable if condition == True]
Example
In the following example, we are going to use list comprehension along with the join().
x = "tp, tutorialspoint" y = ''.join([char.upper() if char.islower() else char for char in x]) print("Result :", y)
Output
Following is the output of the above program ?
Result : TP, TUTORIALSPOINT