lundi 5 avril 2021

how to generalize the given code for all strings in python?

What my aim is that writing a function takes a string as an argument such as "Nisa" and returns "N1I2S3A". But the code that I have written is only works for strings that have only three characters. How do I generalize the code for all strings? If you can help me, I would be really grateful since I am a beginner in python. Here is the code:

tested_str = str(input("Enter a name: "))
def strPattern(mystr):
   while tested_str:
        if len(mystr) == 1:
           return mystr.upper()
        else:
           return (mystr[0] + str("1") + mystr[1:len(mystr) - 1:1].upper() + str("2") 
           + mystr[-1]).upper()

strPattern(mystr=tested_str)

Aucun commentaire:

Enregistrer un commentaire