2

How do I add space between a string and an integer, and also if i want to print in new line? This is my code, so output will be, 1000normal, but my output should be like this

1000 normal

or

1000
normal

How do I achieve this? Below is code attempt.

w = 40
h = 5
b = (w * (pow(h, 2)))
f1 = "normal"
f2 = "average"
if b >= 500:
    print(str(b)+f1)
else:
    print(str(b)+f2)

2 Answers 2

4

The most basic way would be to use string concatenation:

print(str(b) + ' ' + f1)

But note the default sep parameter for print is a single whitespace. So you can just use:

print(str(b), f1)            # single space separator
print(str(b), f1, sep='\n')  # new line separator

With Python 3.6+ you can use formatted string literals (PEP 498):

print(f'{b} {f1}')           # single space separator
print(f'{b}\n{f1}')          # new line separator
Sign up to request clarification or add additional context in comments.

Comments

2

For space:

print(str(b), f1)

For printing with a newline between b and f1:

print(str(b) + '\n' + f1)

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.