Convert a user input string to uppercase in Python

avatar

Borislav Hadzhiev

Last updated: Aug 25, 2022

banner

Photo from Unsplash

Convert a user input string to uppercase in Python #

Use the str.upper() method to convert a user input string to uppercase, e.g. uppercased = user_input.upper(). The str.upper() method returns a copy of the string with all the cased characters converted to uppercase.

main.py
user_input = input('Do you like pasta (YES/NO): ') if user_input.upper() == 'YES': print('user typed yes') elif user_input.upper() == 'NO': print('user typed no') else: print('Type YES or NO') # 👇️ use uppercase method to convert to uppercase uppercase = user_input.upper() print(uppercase) # 👉️ YES # 👇️ use capitalize method to uppercase first letter and lowercase rest capitalized = user_input.capitalize() print(capitalized) # 👉️ Yes

user input convert uppercase

We used the input() function to take input from the user.

The input function takes an optional prompt argument and writes it to standard output without a trailing newline.

The str.upper method returns a copy of the string with all the cased characters converted to uppercase.

main.py
print('yes'.upper()) # 👉️ 'YES' print('YES'.capitalize()) # 👉️ 'Yes' print('YES'.lower()) # 👉️ 'yes'

The method doesn't change the original string, it returns a new string. Strings are immutable in Python.

If you need to convert the first letter in the string to uppercase and the rest to lowercase, use the str.capitalize() method.

The str.capitalize function returns a copy of the string with the first character capitalized and the rest lowercased.

If you need to check if the user entered one of multiple values, use the in operator.

main.py
user_input = input('Do you like pasta (YES/NO): ') yes_choices = ['YES', 'Y'] no_choices = ['NO', 'N'] if user_input.upper() in yes_choices: print('user typed yes') elif user_input.upper() in no_choices: print('user typed no') else: print('Type yes or no')

input uppercase check if one of multiple

We used the in operator to check if the input value is either of the items in the list.

The in operator tests for membership. For example, x in l evaluates to True if x is a member of l, otherwise, it evaluates to False.

If you only want to allow the user to enter specific values, use a while loop.

main.py
yes_choices = ['YES', 'Y'] no_choices = ['NO', 'N'] while True: user_input = input('Do you like pasta (YES/NO): ') if user_input.upper() in yes_choices: print('user typed yes') break elif user_input.upper() in no_choices: print('user typed no') break else: print('Type yes or no') continue

We used a while loop to only allow the user to answer YES, Y, NO or N.

If the if block runs, we print a message and use the break statement to exit out of the loop.

The break statement breaks out of the innermost enclosing for or while loop.

If the user entered an invalid value, the else block runs, where we use the continue statement to prompt the user again.

The continue statement continues with the next iteration of the loop.

When validating user input in a while loop, we use the continue statement when the input is invalid.

If the input is valid, we use the break statement to exit out of the while loop.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.