Python program to uppercase the given characters


Text processing often requires manipulating the case of characters in a string. One common task is converting lowercase characters to uppercase. In Python, there are built-in functions and methods that simplify this task. In this article, we will explore how to write a Python program to convert characters to uppercase.

Uppercasing characters is essential for various applications, such as data cleaning, text analysis, and string matching. By converting characters to uppercase, we can ensure uniformity, improve readability, and enable effective comparison and matching operations.

In this article, we will discuss the problem statement, explore the approach and algorithm to uppercase characters, and provide a step-by-step implementation guide with code explanations. We will also include sample test cases to validate the program's functionality.

Understanding the Problem

Before we start writing the code, let's understand the problem statement in detail. We need to create a Python program that takes a string as input and converts all the lowercase characters in the string to uppercase.

To achieve this, we need to iterate over each character in the string and check if it is a lowercase letter. If it is, we will convert it to uppercase and update the string accordingly. Finally, we will return the modified string as the output.

Approach and Algorithm

To solve this problem, we can follow the following algorithm −

  • Accept the input string from the user.

  • Initialize an empty string to store the modified string.

  • Iterate over each character in the input string.

  • Check if the character is a lowercase letter using the islower() method.

  • If the character is a lowercase letter, convert it to uppercase using the upper() method and append it to the modified string.

  • If the character is not a lowercase letter, append it to the modified string as it is.

  • After iterating over all the characters, the modified string will contain the desired uppercase version of the input string.

  • Return the modified string as the output.

This approach ensures that we only convert lowercase characters to uppercase, leaving other characters unchanged.

Implementation Converting Characters to Uppercase

Now that we have understood the problem and have an approach in mind, let's proceed with the implementation. We will write a Python function called convert_to_uppercase that takes a string as input and returns the modified string with all lowercase characters converted to uppercase.

def convert_to_uppercase(input_string):
    modified_string = ""
    for char in input_string:
        if char.islower():
            modified_string += char.upper()
        else:
            modified_string += char
    return modified_string

Let's break down the implementation step-by-step 

  • We define a function convert_to_uppercase that takes input_string as a parameter.

  • We initialize an empty string called modified_string to store the modified version of the input string.

  • We iterate over each character, char, in the input_string.

  • Inside the loop, we use the islower() method to check if char is a lowercase letter.

  • If char is a lowercase letter, we convert it to uppercase using the upper() method and append it to modified_string.

  • If char is not a lowercase letter, we simply append it to modified_string without any changes.

  • After iterating over all the characters in the input string, we return the modified_string as the output

Now that we have implemented the function, let's test it with a few examples to see how it performs.

In the next section, we will provide multiple test cases along with the expected output to demonstrate the working of the program.

Example Test Cases

To demonstrate the functionality of our program, let's consider multiple test cases with different input strings. We will provide the input strings along with the expected output.

Test Case 1

Input: "Hello, World!" Expected Output: "HELLO, WORLD!"

Explanation  In this case, the input string contains both uppercase and lowercase letters. Our program should convert all the lowercase letters to uppercase while leaving the uppercase letters unchanged. The expected output is "HELLO, WORLD!".

Test Case 2

Input: "python" Expected Output: "PYTHON"

Explanation  Here, the input string consists of all lowercase letters. The program should convert all the lowercase letters to uppercase. The expected output is "PYTHON".

Test Case 3

Input: "UPPERCASE" Expected Output: "UPPERCASE"

Explanation  In this case, the input string contains only uppercase letters. Since there are no lowercase letters, our program should return the input string as it is. The expected output is "UPPERCASE".

Test Case 4

Input: "123" Expected Output: "123"

Explanation  This test case consists of numeric characters. Since our program is designed to convert only lowercase letters to uppercase, it should not make any changes to the numeric characters. Therefore, the expected output is "123".

Conclusion

In this tutorial, we have learned how to write a Python program to uppercase the given characters in a string. We explored two different approaches: using the str.upper() method and iterating through each character of the string.

By using the str.upper() method, we were able to easily convert all characters in the string to uppercase. This approach is simple and concise, suitable for scenarios where the entire string needs to be converted.

Alternatively, by iterating through each character and using the ord() and chr() functions, we achieved the same result. This approach provides more flexibility, allowing us to selectively convert specific characters while leaving others unchanged.

Updated on: 10-Aug-2023

50 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements