Knowee
Questions
Features
Study Tools

Complete the following code to find lowercase and uppercase letters from a given string using regular expressions. Sample input:WelcometoINDIASample output:[‘W’, ‘I’, ‘N’, ‘D’. ‘I’, ‘A’][‘e’, ‘l’, ‘c’, ‘o’, ‘m’, ‘e’, ‘t’, ‘o’]

Question

Complete the following code to find lowercase and uppercase letters from a given string using regular expressions. Sample input:WelcometoINDIASample output:[‘W’, ‘I’, ‘N’, ‘D’. ‘I’, ‘A’][‘e’, ‘l’, ‘c’, ‘o’, ‘m’, ‘e’, ‘t’, ‘o’]

🧐 Not the exact question you are looking for?Go ask a question

Solution 1

To find lowercase and uppercase letters from a given string using regular expressions, you can follow these steps:

  1. Import the re module to work with regular expressions.
  2. Define a function, let's call it find_letters, that takes a string as input.
  3. Use the re.findall() function to find all uppercase letters in the string. The regular expression pattern for uppercase letters is [A-Z].
  4. Use the re.findall() function again to find all lowercase letters in the string. The regular expression pattern for lowercase letters is [a-z].
  5. Return the list of uppercase letters and lowercase letters as the output.

Here's the complete code:

import re

def find_letters(string):
    uppercase_letters = re.findall(r'[A-Z]', string)
    lowercase_letters = re.findall(r'[a-z]', string)
    return uppercase_letters, lowercase_letters

# Sample input
input_string = "WelcometoINDIA"

# Call the function and print the output
output = find_letters(input_string)
print(output)

The output will be:

(['W', 'I', 'N', 'D', 'I', 'A'], ['e', 'l', 'c', 'o', 'm', 'e', 't', 'o'])

This means that the uppercase letters found in the string are 'W', 'I', 'N', 'D', 'I', 'A', and the lowercase letters found are 'e', 'l', 'c', 'o', 'm', 'e', 't', 'o'.

This problem has been solved

Solution 2

To find lowercase and uppercase letters from a given string using regular expressions, you can follow these steps:

  1. Import the regular expressions module:
import re
  1. Define the given string:
string = "WelcometoINDIA"
  1. Use regular expressions to find uppercase letters:
uppercase_letters = re.findall(r'[A-Z]', string)
  1. Use regular expressions to find lowercase letters:
lowercase_letters = re.findall(r'[a-z]', string)
  1. Print the uppercase and lowercase letters:
print(uppercase_letters)
print(lowercase_letters)

The complete code would look like this:

import re

string = "WelcometoINDIA"

uppercase_letters = re.findall(r'[A-Z]', string)
lowercase_letters = re.findall(r'[a-z]', string)

print(uppercase_letters)
print(lowercase_letters)

When you run this code, it will output:

['W', 'I', 'N', 'D', 'I', 'A']
['e', 'l', 'c', 'o', 'm', 'e', 't', 'o']

This means that the uppercase letters found in the given string are 'W', 'I', 'N', 'D', 'I', 'A', and the lowercase letters found are 'e', 'l', 'c', 'o', 'm', 'e', 't', 'o'.

This problem has been solved

Solution 3

To find lowercase and uppercase letters from a given string using regular expressions, you can follow these steps:

  1. Import the regular expressions module:
import re
  1. Define the given string:
string = "WelcometoINDIA"
  1. Use regular expressions to find uppercase letters:
uppercase_letters = re.findall(r'[A-Z]', string)
  1. Use regular expressions to find lowercase letters:
lowercase_letters = re.findall(r'[a-z]', string)
  1. Print the results:
print(uppercase_letters)
print(lowercase_letters)

The complete code would look like this:

import re

string = "WelcometoINDIA"

uppercase_letters = re.findall(r'[A-Z]', string)
lowercase_letters = re.findall(r'[a-z]', string)

print(uppercase_letters)
print(lowercase_letters)

When you run this code, it will output:

['W', 'I', 'N', 'D', 'I', 'A']
['e', 'l', 'c', 'o', 'm', 'e', 't', 'o']

This means that the uppercase letters found in the given string are 'W', 'I', 'N', 'D', 'I', 'A', and the lowercase letters found are 'e', 'l', 'c', 'o', 'm', 'e', 't', 'o'.

This problem has been solved

Similar Questions

How can you convert a lowercase character to uppercase manually in a string?

Write a python program to convert all the letters in the string to uppercasestring = "Hello World"print(string.upper())string = "Hello World"print(string.title())string = "Hello World"print(string.capitalize())string = "Hello World"print(string.Upper())

Which method can be used to return a string in upper case letters?

Write a program that converts strings of all uppercase characters into strings of all lowercase and viceversa

Write a method that takes a String and returns it in AltCase.This means that the first letter is uppercase and each subsequent character alternates between lowercase and uppercase.Here are some examples of words in AltCase:HeLlOHeLlO WoRlDThIs iS In aLtCaSe.Note: if there is a space, you still treat that as a character, so the String "A B" is in AltCase, since technically, the space between the letters is lower case.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.