String in Python

I have to write a program and input a string then it should print 4 line.
1st line condition is it’ll print uppercase only!
2nd line condition is it’ll print lowercase only!
3rd line condition is it’ll print number only!
4th line condition is it’ll print other elements(like space,comma,semi-colon bla bla)

If i take as a input:

Hello Test! 123 123, good.

Then output should be like:

HT
elloestgood
123123
 !  , .

Note: I know about len(),.find(),.replace(),.strip(),.upper(),.lower(),.count(),
.startswith(),endswith(),.capitalize() and also about loop.Is those knowledge enough to solve this problem or i’ve to know more about string?Better to give me a hint,not answer!

Here are some methods that may help you.

>>>my_string = "Hello World!"
>>>my_string[0].isupper()
True
>>>my_string[0].islower()
False
>>>my_string[0].isdigit()
False

>>>from string import punctuation
>>>True if my_string[0] in punctuation else False #if/else written as ternary
False
#Other way of writing instead of ternary
>>>if my_string[0] in punctuation:
>>>    print(True)
>>>else:
>>>    print(False)
#Another hint string.whitespace

To get a quick view on methods on a datatype/module/etc, you can use dir().
For example,

dir("somestring")
import string
dir(string)

Just rember not to instantiate it with the parenthesis.
This is wrong and would evaluate and get the directory on the result:

dir(my_function())

You can also use:

help()

the same way you would use dir()

1 Like

Another approach I would use is list comprehension with a condition check.
For example:

>>>my_string = "Hello World123!"
>>>upper_letters = [letter for letter in my_string if letter.isupper()]
>>>print(upper_letters)
['H', 'W']
# We can also join the list as a string instead of a list if needed, using "join",
#prepend what you would like to use as the spacer in quotes, in my example I use a blank string
#as a spacer between list elements
>>>print("".join(upper_letters))
'HW'
1 Like

This is a solution, don’t remove spoiler unless you want it to be spoiled.

from string import punctuation, whitespace

my_string = "Hello World123!"
upper_letters = "".join([letter for letter in my_string if letter.isupper()])
lower_letters = "".join([letter for letter in my_string if letter.islower()])
digit_chars =    "".join([letter for letter in my_string if letter.isdigit()])
other_elem =   "".join([letter for letter in my_string if letter in punctuation or letter in whitespace])
1 Like

I did this but i lost “space” :sob: :sob: :sob:
Code:

import string
result = string.punctuation
name = input("")
upper_letters = [letter for letter in name if letter.isupper()]
print("".join(upper_letters))
lower_letters = [letter for letter in name if letter.islower()]
print("".join(lower_letters))
digits = [letter for letter in name if letter.isdigit()]
print("".join(digits))
for i in name:
    if i in result:
        print(i,end='')

Input:

Hello Test! 123 123, good.

Output:

HT
elloestgood
123123
!,.

How can i print space also?

[letter for letter in name if letter in string.punctuation or letter in string.whitespace]

1 Like

Thank you so much Gerschel :heart:
Your explanation was really helpful and also help me to know some cool things like: .isupper(),.islower(),.isdigit(),.punctuation(),.whitespace