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
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.