All Courses

What are the different methods to convert a Python string into uppercase? Can you explain with examples?

By Pp5344229@gmail.com, a month ago
  • Bookmark
0

How we can change a Python string into uppercase?

Python
String
Uppercase?
1 Answer
0
Goutamp777

There are several methods to convert a Python string into uppercase:

  1. Using the 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


Your Answer

Webinars

Live Masterclass on : "How Machine Get Trained in Machine Learning?"

Mar 30th (7:00 PM) 516 Registered
More webinars

Related Discussions

Running random forest algorithm with one variable

View More