World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITians, only for AI Learners.
Designed by IITians, only for AI Learners.
New to InsideAIML? Create an account
Employer? Create an account
There are several methods to convert a Python string into uppercase:
upper()
method: This method returns a new string with all characters in uppercase.string = "hello world" upper_string = string.upper() print(upper_string) # Output: HELLO WORLD
2. Using the capitalize() method: This method returns a new string with the first character in uppercase.
string = "hello world" capitalized_string = string.capitalize() print(capitalized_string) # Output: Hello world
3. Using a combination of upper() and string slicing: This method returns a new string with only the first character in uppercase and the rest in lowercase.
string = "hello world" upper_first_string = string[0].upper() + string[1:].lower() print(upper_first_string) # Output: Hello world
4. Using the title() method: This method returns a new string with the first character of each word in uppercase.
string = "hello world" title_string = string.title() print(title_string) # Output: Hello World
5. Using the casefold() method: This method returns a new string with all characters in lowercase and is similar to the lower() method. However, it can also handle some special characters in different languages.
string = "HeLlO wOrLd" casefold_string = string.casefold() print(casefold_string) # Output: hello world