Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python HELP: Write a function that takes a string as an argument, such as "James" and then returns the following string pattern: "J1A2M3E4S".

austin

New Coder
I am trying to write a code that will transform a string to the given pattern. Let me give you more examples.
If we write "miracle", the letters will be upper case (MIRACLE) and after that we will have numbers between the letters -- "M1I2R3A4C5L6E" (There won't be a number after the last letter.)
If we write "bASebALL", again all letters will be upper case, there will be numbers in ascending order between them. -- "B1A2S3E4B5A6L7L"

To solve this, I tried to find a code that will insert something simple in between letters first. Like dashes. (SOURCE: https://stackoverflow.com/questions/29382285/python-making-a-function-that-would-add-between-letters)

>>> def f(s):
m = s[0]
for i in s[1:]:
m += '-' + i
return m
>>> f("James")

But, I couldn't change the code into a code which inserts ascending numbers between letters.

Also I have this piece of code I wrote for upper casing the word.

mystr = 'james'
mystr_up = mystr.upper()
mystr_up

It successfully prints "JAMES". But I don't know how to use this code to create a while or for loop.

Also the code must start like this:
def strPattern(mystr):

and at the end we have to use "return"
 
Solution
Hi austin,

Use the button </> when you post code.

Python:
def strPattern(mystr):
    newStr = ''
    mystr = mystr.upper()

    for c in range(0, len(mystr)-1):
        newStr += mystr[c] + str(c+1)

    if len(mystr) > 1:
        newStr += mystr[len(mystr)-1]
       
    return newStr

print(strPattern("hello"))
Hi austin,

Use the button </> when you post code.

Python:
def strPattern(mystr):
    newStr = ''
    mystr = mystr.upper()

    for c in range(0, len(mystr)-1):
        newStr += mystr[c] + str(c+1)

    if len(mystr) > 1:
        newStr += mystr[len(mystr)-1]
       
    return newStr

print(strPattern("hello"))
 
Solution

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom