Q:

Write a python program to find words with both alphabets and numbers from an input string.

0

Find words with both alphabets and numbers

Write a program to find words with both alphabets and numbers from an input string.

Given:

str1 = "Emma25 is Data scientist50 and AI Expert"

Expected Output:

Emma25
scientist50

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Hint:

Use the built-in function any() with the combination of string functions isalpha() and isdigit()

Solution:

str1 = "Emma25 is Data scientist50 and AI Expert"
print("The original string is : " + str1)

res = []
# split string on whitespace
temp = str1.split()

# Words with both alphabets and numbers
# isdigit() for numbers + isalpha() for alphabets
# use any() to check each character

for item in temp:
    if any(char.isalpha() for char in item) and any(char.isdigit() for char in item):
        res.append(item)

print("Displaying words with alphabets and numbers")
for i in res:
    print(i)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now