What is upper method in python

Метод string lower() и upper() в Python

Метод string lower() преобразует все символы верхнего регистра в строке в символы нижнего регистра и возвращает их.

Параметры

Метод в Python не принимает никаких параметров.

Возвращаемое значение

Команда возвращает строку в нижнем регистре из данной строки. Он преобразует все символы верхнего регистра в нижний регистр.

Если символы верхнего регистра отсутствуют, возвращается исходная строка.

Пример 1: Преобразование строки в нижний регистр

# example string string = "THIS SHOULD BE LOWERCASE!" print(string.lower()) # string with numbers # all alphabets whould be lowercase string = "Th!s Sh0uLd B3 L0w3rCas3!" print(string.lower())
this should be lowercase! th!s sh0uld b3 l0w3rcas3!

Пример метода lower()

# first string firstString = "PYTHON IS AWESOME!" # second string secondString = "PyThOn Is AwEsOmE!" if(firstString.lower() == secondString.lower()): print("The strings are same.") else: print("The strings are not same.")

Примечание: Если вы хотите преобразовать строку в верхний регистр, используйте upper(). Вы также можете использовать метод swapcase() для переключения между нижним регистром и верхним регистром.

Читайте также:  Get group regexp javascript

Метод string upper() преобразует все символы нижнего регистра в строке в символы верхнего регистра и возвращает их.

Параметры

Метод upper() в Python не принимает никаких параметров.

Возвращаемое значение

Метод возвращает строку в верхнем регистре из данной строки. Он преобразует все символы нижнего регистра в верхний регистр.

Если строчные символы отсутствуют, возвращается исходная строка.

Пример 1: Преобразование строки в верхний регистр

# example string string = "this should be uppercase!" print(string.upper()) # string with numbers # all alphabets whould be lowercase string = "Th!s Sh0uLd B3 uPp3rCas3!" print(string.upper())
THIS SHOULD BE UPPERCASE! TH!S SH0ULD B3 UPP3RCAS3!

Пример 2: Как в программе используется?

# first string firstString = "python is awesome!" # second string secondString = "PyThOn Is AwEsOmE!" if(firstString.upper() == secondString.upper()): print("The strings are same.") else: print("The strings are not same.")

Примечание: Если вы хотите преобразовать строку в нижний регистр, используйте lower(). Вы также можете использовать swapcase() для переключения между нижним регистром и верхним регистром.

Источник

Python String upper()

The upper() method converts all lowercase characters in a string into uppercase characters and returns it.

Example

message = 'python is fun' 
# convert message to uppercase print(message.upper())
# Output: PYTHON IS FUN

Syntax of String upper()

The syntax of upper() method is:

upper() Parameters

upper() method doesn’t take any parameters.

upper() Return Value

upper() method returns the uppercase string from the given string. It converts all lowercase characters to uppercase.

If no lowercase characters exist, it returns the original string.

Example 1: Convert a string to uppercase

# example string string = "this should be uppercase!" 
print(string.upper())
# string with numbers # all alphabets should be lowercase string = "Th!s Sh0uLd B3 uPp3rCas3!"
print(string.upper())
THIS SHOULD BE UPPERCASE! TH!S SH0ULD B3 UPP3RCAS3!

Example 2: How upper() is used in a program?

# first string firstString = "python is awesome!" # second string secondString = "PyThOn Is AwEsOmE!" 
if(firstString.upper() == secondString.upper()):
print("The strings are same.") else: print("The strings are not same.")

Note: If you want to convert to lowercase string, use lower(). You can also use swapcase() to swap between lowercase to uppercase.

Источник

Python Upper function

Python is a high-level programming language that includes many built-in functions designed to simplify programming tasks. The Python upper function, which converts all lowercase letters in a string to uppercase, is one of these built-in functions. In this article, we will look at the python upper function, its syntax, return value, and some examples.

What is the Upper() Function in Python?

The python upper function is a built-in function in Python that is used to convert all lowercase letters in a string to uppercase. It does not modify the original string; instead, it returns a new string with all the lowercase letters converted to uppercase. The upper() function is part of the string class in Python and can be used on any string object.

Syntax of the Upper() Function in Python

The syntax for the python upper function is quite simple and easy to understand. Here is the basic syntax:

In the syntax above, string is the name of the string that you want to convert to uppercase using the upper() function.

Return Value of the Upper() Function in Python

The upper() function in Python returns a new string with all the lowercase letters in the original string converted to uppercase. If the original string is already in uppercase, the upper() function returns the same string without any modifications.

Time Complexity of Python Upper Function

The time complexity of the python upper function is O(n), where n is the length of the input string. This is because the function needs to iterate through each character in the string to check if it is alphabetic and convert it to uppercase if necessary. Since the function needs to check each character in the string, its time complexity scales linearly with the length of the input string.

Examples of the Python Upper Function in Python

To give you a better idea of how the upper() function works in Python, let’s take a look at some examples.

Example 1: Convert a string to uppercase using the upper() function
In this example, we will use the upper() function to convert a string to uppercase. Here is the code:

# your code goes here # Convert a string to uppercase using the upper() function string = "hello world" upper_string = string.upper() # Print the original string and the uppercase string print("Original String:", string) print("Uppercase String:", upper_string)
Original String: hello world Uppercase String: HELLO WORLD

In the code above, we created a string variable named string that contains the string «hello world.» We then used the upper() function to convert the string to uppercase and store the result in a new variable called upper_string. Finally, we printed both the original string and the uppercase string to the console.

Example 2: Using the python upper function with user input
In this example, we will take input from the user and convert it to uppercase using the upper() function. Here is the code:

# your code goes here # Using the upper() function with user input string = input("Enter a string: ") upper_string = string.upper() # Print the original string and the uppercase string print("Original String:", string) print("Uppercase String:", upper_string)
Enter a string: Original String: Hello prepbytes Uppercase String: HELLO PREPBYTES

In the code above, we used the input() function to get string input from the user. We then used the upper() function to convert the string to uppercase and stored the result in a new variable called upper_string. Finally, we printed both the original string and the uppercase string to the console.

Example 3: Using the python upper function to convert a list of strings to uppercase

# your code goes here my_list = ["hello", "world", "example"] # Using list comprehension to create a new list with uppercase strings uppercase_list = [string.upper() for string in my_list] print(uppercase_list)

Explanation
In this code, we first define a list of strings called my_list. Then we use a list comprehension to create a new list called uppercase_list that contains the uppercase versions of each string in my_list. The upper() method is called on each string to convert it to uppercase. Finally, we print the resulting uppercase_list.

Summary
The python upper function in Python is a built-in string method that returns a new string with all alphabetic characters converted to uppercase. It does not modify the original string but creates a new string with uppercase characters. The upper() function can be called on a string variable using the dot notation and is case-sensitive, meaning that it only converts lowercase alphabetic characters to uppercase, and leaves uppercase characters unchanged. The upper() function can be used alongside other string methods and functions to manipulate strings in Python. It cannot be directly used on a list of strings, but a list comprehension or loop can be used to apply the upper() function to each string in the list.

Frequently Asked Questions(FAQ)

Sure, here are some frequently asked questions about the Python upper function:

Q1: How do you use the upper() function in Python?
Ans: The upper() function is called on a string variable using the dot notation. For example, my_string.upper() returns a copy of my_string with all alphabetic characters in uppercase.

Q2: Does the upper() function modify the original string?
Ans: No, the upper() function does not modify the original string. It returns a new string with all alphabetic characters in uppercase.

Q3: What is the difference between upper() and lower() in Python?
Ans: The upper() and lower() functions are both string methods in Python, but they perform opposite actions. The upper() function converts all alphabetic characters in a string to uppercase, while the lower() function converts them to lowercase.

Q4: Can the upper() function be used with non-alphabetic characters?
Ans: Yes, the upper() function will not modify non-alphabetic characters in a string. Only alphabetic characters will be converted to uppercase.

Q5: Is the upper() function case-sensitive?
Ans: Yes, the upper() function is case-sensitive. It only converts lowercase alphabetic characters to uppercase and leaves uppercase alphabetic characters unchanged.

Q6: Can the upper() function be used on a list of strings?
Ans: No, the upper() function can only be used on a string. If you have a list of strings that you want to convert to uppercase, you can use a list comprehension or loop to apply the upper() function to each string in the list.

Источник

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