'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.
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 1:[1]
you can slice a string by using this method
string[len(string)//2:] #2nd half of the string
string[2:len(string)//2] #1nd half of the string
string[:-1] string without the last charakter
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | gerda die gandalfziege |