Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add "?" sign if the sentence type is questions

I have a list. In this list have various type of sentences suppose question type and general sentences. I want to add question mark "?" sign after end of the sentence if it it is question.

Here is my list

lst = ['what is Fame', 'how can you start work', 'can you learn design', 'this is text']

I tried this code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for i in lst:
if 'what' or 'how' or 'can' in lst:
    print(i, '?')

But not getting my desired output.

I would like to achieve:

what is Fame?
how can you start work?
can you learn design?

Could anyone please help me to do this?

Thanks, everyone!

>Solution :

You can achieve the desired output by checking if sentence in list startswith with a specific word. If yes, you can print a question mark at the end:

lst = ['what is Fame', 'how can you start work', 'can you learn design', 'this is text']

for phrase in lst:
    if phrase.startswith('what') or phrase.startswith('how') or phrase.startswith('can'):
        print(phrase + '?')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading