Python str to upper case

Convert String to Uppercase in Python

To convert a string to uppercase in Python use the upper() method. The upper() method is a built-in Python function that converts all lowercase characters in a string to uppercase and returns the resulting string.

In this article, I will explain the syntax of the python string upper() method, its parameters and explain how to convert a string to uppercase in Python.

Читайте также:  Как обойти капчу при парсинге на питоне

1. Quick Examples of Convert String to Uppercase

If you are in a hurry, below are some quick examples of how to convert string to uppercase in python.

 # Below are the quick examples #Example 1: Convert string to uppercase stringVar = "welcome to sparkbyexamples" print(stringVar.upper()) #Example 2: Convert literal string to uppercase result = "welcome to sparkbyexamples".upper()) print(result) #Example 3: Convert string to uppercase stringVar = "welcome to sparkbyexamples" stringVar = stringVar.upper() print(stringVar) #Example 4: Convert alphanumeric string to uppercase stringVar = "we1c0m4 t0 5pa6k8yex0mp7e8" print(stringVar.upper()) #Example 5: Convert part of the string uppercase_string = stringVar[:10] + stringVar[10:].upper() print(uppercase_string) #Example 6: Convert first letter to Upper stringVar = "welcome to sparkbyexamples" print(stringVar.capitalize()) 

2. Syntax of String upper() Method

Following is the syntax for creating upper() method

 # Syntax of upper() function string.upper() 

2.1 Parameter of upper()

The upper() method does not take any parameters.

3. Convert a String to Uppercase in Python

You can use the upper() function to convert characters in a string to uppercase. The upper() function returns a new string with all the characters in the original string converted to uppercase. It does not modify the original string. For example,

 # Convert string to uppercase stringVar = "welcome to sparkbyexamples" print(stringVar.upper()) # Output # WELCOME TO SPARKBYEXAMPLES 

4. Convert Alphanumeric String to Upper case

Similarly, you can also convert alphanumeric string to uppercase using this method. For an alphanumeric string, it converts only lowercase letters to uppercase and leaves the other character unchanged.

 # Convert string to uppercase using upper() stringVar = "we1c0m4 t0 5pa6k8yex0mp7e8" print(stringVar.upper()) # Output # WE1C0M4 T0 5PA6K8YEX0MP7E8 

5. Convert the Original Input String to Uppercase

Note that the upper() method does not modify the original string instead it returns a new string with the uppercase version of the original string. If you want to modify the original string, you can assign the result of the upper() method back to the original string. For example,

 # Convert string to uppercase stringVar = "welcome to sparkbyexamples" result = stringVar.upper() print(result) # Output # WELCOME TO SPARKBYEXAMPLES 

6. Convert Part of the String to Uppercase

Sometimes you would be required to convert only part of the string, you can achieve this by splitting the string and converting part of the string to upper case, and concatenating the strings.

The below example converts only a part of the string to uppercase, Here, I used slicing and concatenation.

 # Convert Part of the String to Upper stringVar = "welcome to sparkbyexamples" uppercase_string = stringVar[:10] + stringVar[10:].upper() print(uppercase_string) # Output: # welcome to SPARKBYEXAMPLES 

7. Convert the First Letter to Upper case

You can also use the str.capitalize() method to convert the first character of a string to uppercase and the rest to lowercase.

 # Convert First Letter of string to Upper stringVar = "welcome to sparkbyexamples" print(stringVar.capitalize()) # Output: # Welcome to sparkbyexamples 

8. Comparison of Strings Using upper() Method

Similarly, you can use the upper() method to compare two strings for equality, ignoring the case of the characters. This function is used to convert both strings to uppercase before the comparison.

 # Comparison of strings using upper() method String1 = "welcome to sparkbyexamples" String2 ="WeLComE TO SpArKbYExAmPlEs" if(String1.upper() == String2.upper()): print("Strings are same") else: print("Strings are not same") # Output # Strings are same 

Conclusion

In this article, I have explained the Python string upper() method syntax, parameters, and how to convert all lowercase characters in a string to uppercase and also explained several examples like converting title case, converting on original string e.t.c.

You may also like reading:

Источник

7 Ways of Making Characters Uppercase Using Python

python uppercase

The reason why python has become such a popular programming language is that it provides programmers lots and lots of versatile and standard libraries that are readily available means we don’t even need to install them explicitly. One such library in python is upper(), which converts strings in python to uppercase.

It means if we have a string in lower letters (For example – ” hello, how are you”), we can use upper() to convert it into uppercase letters (“HELLO, HOW ARE YOU”). We will not only know how to turn all the letters to uppercase, but we will also know how to convert Only the First letter and every alternative letter to uppercase.

Syntax of Making Characters Uppercase Using Python

As upper() is a built-in method we do not even need to import it. We can use it directly like this –

string="hello" uppercase_string=string.upper() print(uppercase_string)

python uppercase

Note- It does not take any arguments.

Examples of converting string in python uppercase

  • The first letter in the string capital
  • First letter of each word Capital
  • To match if two strings are the same
  • Check if the String is already in uppercase or not
  • To make every alternative character in uppercase letters
  • Converting String to Python Uppercase without built-in function
  • Conversion of String from Python Uppercase to Lowercase

1. The First Letter in the string capital in Python

For this purpose, we have a built-in function named capitalize()

string="hello how are you" uppercase_string=string.capitalize() print(uppercase_string)

python uppercase

Sometimes we forget how the function name, so we should also know how to do the same without using these functions.

string="hello how are you" new_string="" for i in range(len(string)): if i==0: new_string+=string[i].upper() else: new_string+=string[i] print(new_string)

2. The First Letter of each word Capital in Python

Suppose we want all the words in a string to be uppercase. For this we have a method available in python called title().

string="hello how are you" uppercase_string=string.title() print(uppercase_string)

python uppercase

We can do this without using built-in function – title() like this-

string="my name is ashwini mandani" list1=string.split(" ") for i in range(len(list1)): list1[i]=list1[i].capitalize() string=" ".join(list1) print(string)
Output- My Name Is Ashwini Mandani

3. To match if two strings are the same

If we want to compare two strings in terms of whether they are same or not (not considering the uppercase and lowercase aspect).

string1="thiS iS a STrinG" string2="ThiS iS a sTrING" if string1.upper()==string2.upper(): print("Both the strings are same") else: print("not same")
Output- Both the strings are same

4. To check if the String is already in uppercase or not

There are many times when we are taking input from the user and it is not compulsory that every user inputs in the same format. But we need to store the data in the same format, therefore if the string is already in uppercase print ‘already in upper case’ otherwise convert it into uppercase.

def string_check(string): if string.isupper(): return("Already in UpperCase") else: string=string.upper() return (string) print(string_check("MY NAME IS ASHWINI")) print(string_check("my name is ashwini"))
Output- Already in UpperCase MY NAME IS ASHWINI

5. To make every alternative character in uppercase letters

string="every alternative character is in uppercase" new_string="" for i in range(len(string)): if i%2==0: new_string+=string[i].upper() else: new_string+=string[i] print(new_string)
Output- EvErY AlTeRnAtIvE ChArAcTeR Is iN UpPeRcAsE

6. Converting String to Python Uppercase without built-in function

We can convert any string into uppercase without using any built-in function. Every character has an ASCII value. Like ‘A’ = 65, ‘B’ = 66, ‘a’ = 97, ‘b’ = 98. We can take advantage of this fact and convert lowercase characters to uppercase characters.

Note that, the ASCII value of ‘A’ – ‘Z’ ranges from 65-90 and that of ‘a’ -’z’ range from 97-122.

string="every charaCter is in uppercase" new_string="" for i in string: # 'ord' is used to find the ascii value if ord(i) >=97 and ord(i)< 123: #'chr' used to find the character from ascii value new_string+=chr(ord(i)-32) else: new_string+=i print(new_string)
Output- EVERY CHARACTER IS IN UPPERCASE

7. Conversion of String from Python Uppercase to Lowercase

Python also provides some counterparts of its upper() and isupper() methods. In Python uppercase characters can be converted into lowercase using lower().

string="ABCDEFGHIJKLMNOPQRSTUVWXYZ" string=string.lower() print(string)
Output- abcdefghijklmnopqrstuvwxyz

Similarly, if we want to check if the string is already in lowercase or not we use islower().

print('abcdefghijklmnopqrstuvwxyz'.islower())

Every other functionality works same for lower() as we have discussed in upper().

Must Read:

Conclusion

Python built-in functions provide a way to convert String in python from uppercase to lowercase and vice versa. Generally, when we want to keep all the text data in the same format (uppercase or lowercase), we use these methods.

Try to run the programs on your side and let me know if you have any queries.

Happy Coding!

Источник

Python Uppercase - All you need to know

In this short tutorial, we are going to look into what Python’s uppercase methods are. We will also consider some examples to clearly understand the concepts.

Table of Contents - Python Uppercase

An introduction to Python uppercase

While working with strings, there might be instances where we need to convert lowercase strings to uppercase. Or we might have to check if a given string is uppercase or not. Python facilitates this with the following functions:

The Python upper() method is used to convert lowercase letters in a string to uppercase. The isupper() method, on the other hand, returns True if all the letters in a string are uppercase.

A real-life use-case of this function is an amusement park ticketing application. You may want to convert all the names of the visitors to uppercase format before printing them on the ticket. This helps with uniformity in storage and easy readability during the ticket check process.

Syntax and parameters

The syntax of the method upper() is:

As you can see, the method does not take any parameters. In the place of string we enter the name of the variable containing the string.

Input:

# Python program to show the working of the upper() function visitor007 = 'Flexiple' print("Original String:") print(visitor007) # upper() function converts string to uppercase print("\nModified String:") print(visitor007.upper())

Output:

Original String: Flexiple Modified String: FLEXIPLE

In the above code snippet, we declare a variable called visitor007 . This variable stores the name of our amusement park visitor. Secondly, we use Python’s upper() method to convert the visitor007 to uppercase. Finally, the revised name is printed.

The upper() method does not affect whitespace, numbers or symbols with a string. This is because those characters aren’t upper/lower case based. Let’s look at an example for the same.

Input:

# Python program for upper() function visitor007 = 'F1exip1e' print("The Original String:") print(visitor007) # upper() function converts string to uppercase print("\nModified String:") print(visitor007.upper())

Output:

The Original String: F1exip1e Modified String: F1EXIP1E

Python isupper with examples

Before we do convert a string to uppercase, we might have to check if it is already in uppercase . The isupper() method checks every case-based character. If it finds any characters in lowercase it returns False, else it returns True.

Input:

visitor_name001 = "Flexiple" print(visitor_name001.isupper()) visitor_name002 = "FLEXIPLE" print(visitor_name002.isupper())

Output:

In the output of our first print statement, the method has returned False . This is because the function found lowercase characters in the string. Whereas, the second print statement has printed out True . This is because the string contains only uppercase letters.

Closing thoughts

Switching between the character cases is a common string operation in Python. In this Python tutorial, we looked at how the upper() method is used. We learned how the isupper() method is used to check a given string. We also looked into a couple of examples to understand both the methods completely.

Источник

Оцените статью