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’]
Solution 1
To find lowercase and uppercase letters from a given string using regular expressions, you can follow these steps:
- Import the
remodule to work with regular expressions. - Define a function, let's call it
find_letters, that takes a string as input. - Use the
re.findall()function to find all uppercase letters in the string. The regular expression pattern for uppercase letters is[A-Z]. - Use the
re.findall()function again to find all lowercase letters in the string. The regular expression pattern for lowercase letters is[a-z]. - 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'.
Solution 2
To find lowercase and uppercase letters from a given string using regular expressions, you can follow these steps:
- Import the regular expressions module:
import re
- Define the given string:
string = "WelcometoINDIA"
- Use regular expressions to find uppercase letters:
uppercase_letters = re.findall(r'[A-Z]', string)
- Use regular expressions to find lowercase letters:
lowercase_letters = re.findall(r'[a-z]', string)
- 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'.
Solution 3
To find lowercase and uppercase letters from a given string using regular expressions, you can follow these steps:
- Import the regular expressions module:
import re
- Define the given string:
string = "WelcometoINDIA"
- Use regular expressions to find uppercase letters:
uppercase_letters = re.findall(r'[A-Z]', string)
- Use regular expressions to find lowercase letters:
lowercase_letters = re.findall(r'[a-z]', string)
- 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'.
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.
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.