“”"
print(‘Hello’ + 10*’ ’ + ‘World’)
“”"
While this code is working fine, I’m having error if I replace the str with float or int using the same print method.
I need help.
Thanks in anticipation
“”"
print(‘Hello’ + 10*’ ’ + ‘World’)
“”"
While this code is working fine, I’m having error if I replace the str with float or int using the same print method.
I need help.
Thanks in anticipation
I’m not sure what you want to know and I can’t tell from your snippet. When I do some variations, I get
print with 10 empty strings:
>>> print('hello' + 10*'' + 'world')
helloworld
print with 10 spaces:
>>> print('hello' + 10*' ' + 'world')
hello world
strings can only combine with strings(+
means string concatenation in this context, not addition):
>>> print('hello' + 10 + 'world')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
unless you help them out:
>>> print('hello' + str(10) + 'world')
hello10world
or you can fill in the blanks:
>>> print('hello{}world'.format(10))
hello10world
print()
will try to print whatever you give it, but you have to make sure the types of different items can be combined everywhere, not just in print()
.
Thanks for your time.
But what I want to achieve is that what method can I use if the ‘Hello’ and ‘World’ have been floating number and integer and which can still maintain that spacing method.
Thanks
I’m not really sure what you are asking either. Do you want to print two numbers separated by 10 spaces?
print(42, 10*' ', 3.14159)
If you are not going to perform math on the numbers it doesn’t really matter if they are strings or numbers.
The solution you gave is exactly what I wanted.
Thanks for your time