Introduction
Converting a string to uppercase is a common operation in programming, useful for standardizing text data. This tutorial will guide you through creating a Python program that converts a given string to uppercase.
Example:
-
Input:
hello world
-
Output:
HELLO WORLD
-
Input:
Python Programming
-
Output:
PYTHON PROGRAMMING
Problem Statement
Create a Python program that:
- Takes a string as input.
- Converts the string to uppercase.
- Displays the uppercase string.
Solution Steps
- Take Input from the User: Use the
input()
function to get a string from the user. - Convert the String to Uppercase: Use the
upper()
method to convert the string to uppercase. - Display the Uppercase String: Use the
print()
function to display the converted string.
Python Program
# Python Program to Convert String to Uppercase
# Author: https://www.rameshfadatare.com/
# Step 1: Take input from the user
input_string = input("Enter a string: ")
# Step 2: Convert the string to uppercase
uppercase_string = input_string.upper()
# Step 3: Display the uppercase string
print(f"The uppercase string is: {uppercase_string}")
Explanation
Step 1: Take Input from the User
- The
input()
function prompts the user to enter a string. The input is stored in the variableinput_string
.
Step 2: Convert the String to Uppercase
- The
upper()
method is used to convert the entire string to uppercase. This method returns a new string with all characters in uppercase.
Step 3: Display the Uppercase String
- The
print()
function is used to display the converted uppercase string.
Output Example
Example 1:
Enter a string: hello world
The uppercase string is: HELLO WORLD
Example 2:
Enter a string: Python Programming
The uppercase string is: PYTHON PROGRAMMING
Example 3:
Enter a string: learning is fun
The uppercase string is: LEARNING IS FUN
Conclusion
This Python program demonstrates how to convert a string to uppercase using the upper()
method. It’s a straightforward example that helps beginners understand basic string manipulation in Python.