Assume you have a string, such as \"Hello 34215 World 5620 From 384 Carsonville\
ID: 663964 • Letter: A
Question
Assume you have a string, such as "Hello 34215 World 5620 From 384 Carsonville"
Your mission will be to do the following:
1. Use a list comprehension to separate out the words from the numbers, and separate them with a space.
2. Use a list comprehension to separate out the digits from the words, and put them together in order from smallest to largest.
3. Print out both of these, separated by a “|”. The output for the example given above would be:
>>>
Hello World From Carsonville | 012233445568
4. Do this using as few lines of code as possible. The best solution uses 2 lines, including the definition of the string.
This problem should be done in Python, using IDLE.
Please make sure your code has been tested and functions properly!
Explanation / Answer
string = "Hello 34215 World 5620 From 384 Carsonville"
print str(" | ".join(["".join([letter for letter in string if letter.isalpha() or letter.isspace()]), "".join(sorted([num for num in string if num.isdigit()]))])).replace(" ", " ")
OUTPUT:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.