0
def print_numbers2():
    for a in range(1,6):
        c = 5-a
        print("." * c , str(a) *a)

My output is

.... 1
... 22
.. 333
. 4444
55555

But output I was expecting to get was without space between dot(.) and number. There is space between dots and numbers.

How can i fix this problem?

1
  • 1
    Replace the ',' with a '+' in the print statement. That will concatenate the two strings directly together (as they are both strings, this doesn't work if that are not) without a space. Commented Nov 21, 2019 at 12:25

6 Answers 6

3

just set sep to an empty string:

def print_numbers2():
    for a in range(1,6):
        c = 5-a
        print("." * c , str(a) *a, sep='')
Sign up to request clarification or add additional context in comments.

Comments

1

Try this :

def print_numbers2():
    for a in range(1,6):
        c = 5-a
        print("." * c +  str(a) *a)

Output :

>>> print_numbers2()
....1
...22
..333
.4444
55555

You need to join the output as a string in stead printing side by side as two different string.

Comments

1

use + instead of , in the print.

Comments

1

Try this -

def print_numbers2():
    for a in range(1,6):
        c = 5-a
        print("."*c,str(a)*a,sep='')
print_numbers2()
....1
...22
..333
.4444
55555

It works for me.

Comments

1

Many methods already provided.

One more using fstrings :

def print_numbers2():
    for a in range(1, 6):
        c = 5-a
        print(f"{'.' * c}{str(a) * a}")

Comments

1

All you have to do is replace comma with a plus sign in your print statement. Like this,

print("." * c + str(a) *a)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.