Write a python program that will take a string as input from the user. The input string should have a combination of BOTH the alphabets and the digits

Ridwan

Write a python program that will take a string as input from the user. The input string should have a combination of BOTH the alphabets and the digits. Then, your task is to identify the digits from that input string and store those digits in a list. Finally, sort the list and print the sorted list and the sum of digits as output to the user.

Sample input 1 m4gt567q09y2

Sample Output 1 ['0', '2', '4', '5', '6', '7', '9']

33

Sample input 2 954217

Sample output 2 There's no alphabet in the string.

I tried doing this far. my code can only satisfy the 1st sample input.But I can't figure out any way to satisfy the sample input 2. I have provided my code below

string1=input("Enter the string: ")
output_list=[] 
sum=0
flag= False
for i in range(len(string1)):
   if string1[i].isdigit():
       output_list.append(string1[i])

output_list.sort()
print(output_list)
for i in output_list:
    sum+=int(i)
print(sum)
Ann Zen

The str.isalpha() method will return True is a string is all alphabetical characters, and the str.isdigit() method will return True if a string is all digits:

string1 = input("Enter the string: ")
if any(map(str.isalpha, string1)):
    output_list = [i for i in string1 if i.isdigit()]
    print(sorted(output_list))
    print(sum(map(int, output_list)))
else:
    print("There's no alphabet in the string.")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

python program to write to json from input

extracting digits from input string c++

Write user input string to file in C

Checking if a user input that should be an int is a string

Python, Split the input string on elements of other list and remove digits from it

Looping program based on user string input not gett

Take Input Dynamically from user in Python Dictionary

Ensure that input value is string of alphabets in reactjs

Take string as input from user by %c and %s and confirm that both the strings are equal

I'm writing a code to take a user input and provide its documentation in python. But the string in the user input is used with quotes in python

Need to take multiple digit input from a string removing spaces and alphabets

How to take user input from HTML form to C program

counting sort with input string having alphabets and numbers

Unable to take full String input from console

I have a program to find the amount of alphabets in a string but its not complete can you complete it python

Changing a string from user input

My Java program refuses to take a string input

How to pass an input string from user to a MIPS program

How can write python code to take input from user via bash script

how to use BufferedReader class to take character and string input from user?

Discord.js have bot take in input from a user posting digit string

java couldn't take 2 continuous String input from user

Explain how to make a program to take a string as input and output its reverse?

Converting utf-8 encoded to string from user input in python

How to make strlen() take the string from user input

String manipulation with python and storing alphabets and digits in separate lists

Java check if a scan input is both a specific string and only three digits

How to have input in Python only take in string and not number or anything else only letters

Receiving multiline string from user input in a python telegram bot