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

Python: Print only part of the string when condition is met

I work on code which will give back all what user inserts.
If user inserts secret_password then his next text after it should be formatted.
Format of text should looks like:

john smith , JOHN SMITH , john , joh

john smithn + JOHN SMITH is easy and done below in the 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

Unfortunately, I don’t know how to slice half part or other part of the string once the string can have other number of characters every time when user inserts something.

Could you please, suggest something.
Code is done there is only missing slicing of the string.

print("Program echo v2!")
secret_password = "premium-code-2019"
text = ""
text_formatted = ""

while True:
    user_text = input()
    if user_text == secret_password:
        text = input("Insert the text")  
        text_formatted = text +" "+text.upper()      
        print(tekxt_formatted)
    elif user_text != secret_password:
        print(user_text)
    if user_text == "":
        break

>Solution :

This should be fine:

print("Program echo v2!")
    secret_password = "premium-code-2019"
    text = ""
    text_formatted = ""
   
    while True:
        try:
            user_text = input()
            if user_text == secret_password:
                text = input("Insert the text")
                splitted = text.split(" ")
                text_formatted = "{}, {}, {}, {}".format(
                    text, text.upper(), splitted[0], splitted[0][:-1]
                )
                print(text_formatted)
            elif user_text != secret_password:
                print(user_text)
            if user_text == "":
                break
        except KeyboardInterrupt:
            break
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