Home > Blockchain >  How do I print a string using a for loop but with no spaces?
How do I print a string using a for loop but with no spaces?

Time:10-15

Suppose we have 'My dog is blue', it prints it with spaces. I have tried using the sep, but it doesn't seem to work. I've tried other methods as well such as .replace(' ', '')

N = input()

for i in N:
    print(i, sep = '')

CodePudding user response:

Solution:

for i in N.replace(" ",""): print(i)

CodePudding user response:

The sep parameter defines the character inserted in between each fields of a same print:

print('a', 'b', 4, sep='tutu')
>>> 'atutubtutu4'

If you don't want a default '\n' to be inserted at the end, just use the end parameter and set it to the empty string

print('a', end='')

The documentation is here https://docs.python.org/3/library/functions.html#print

What didn't worked with .replace(' ', '') ?

  • Related