Python: string generation with concatenation


Task is to generate 50 strings of such kind:

hello 1 world
hello 2 world
hello 3 world
hello 4 world
...

Tried such clumsy way (I forgot Python really):

n = 0
while n < 55:
    n += 1
    print ('hello ' + (str)n + ' world' + '\n')

Python swears in return with such error:

SyntaxError: invalid syntax. Perhaps you forgot a comma?

Ok, I’ve made such clumsy, but working version

n = 0
s = ""
x = ""
while n < 55:
    n += 1
    x = str(n)
    s = 'hello ' + x + ' world' + '\n'
    print (s)

Sloppy, of course… but it works! Write in the comments a better way to solve this problem 😉


This entry was posted in Python (en). Bookmark the permalink.

Leave a Reply

🇬🇧 Attention! Comments with URLs/email are not allowed.
🇷🇺 Комментарии со ссылками/email удаляются автоматически.