Python String upper() Function

Python String upper() function converts complete String into Uppercase and returns a new string. Strings are immutable in String, so the original string value remains unchanged.

Key Points :

  • Return Type: String
  • Parametric Values: No parameters can be passed into the upper() function.
  • Converts whole of string into uppercase
  • It does not modify the original string. The modified string can be saved with a new variable name.

Example: Given String – “Have a Nice Day” Or “Have A NICE DAY” Or “have a nice day” Or “Have a nice day”

New String After Using upper() Function: “HAVE A NICE DAY” (for all the above-given strings)


String upper() Syntax

str_name.upper()

Here str_name refers to the input string. And, upper() is inbuilt string function in python.

str_name = "welcome"
print(str_name.upper())   #  WELCOME

String upper() Examples

Case 1: String is in lowercase and may contain number/special characters/whitespaces

str_name = "welcome 2020"
print(str_name.upper())   #  WELCOME  2020

str_name = "welcome @2020"
print(str_name.upper())   #  WELCOME @2020

Case 2: String is in Uppercase and may contain number/special characters/whitespaces

str_name = "WELCOME 2020"
print(str_name.upper())   #  WELCOME  2020

str_name = "WELCOME @2020"
print(str_name.upper())   #  WELCOME @2020

Case 3: Only first alphabet of every word in a string is Uppercase

str_name = "Python"
print(str_name.upper())   #  PYTHON

str_name = "Python 2020"
print(str_name.upper())   #  PYTHON 2020

Case 4: String contains only numbers or special characters

str_name = "2020"
print(str_name.upper())   #  2020

str_name = "@$&"
print(str_name.upper())   #  @$&

Case 5: String is empty

str_name = ' '
print(str_name.upper())   #  (Will not give any error and show empty space as output)

References