NBowman6
NBowman6

Reputation: 39

Add period to python statement without a space

I am only three weeks into my Intro to Programming course, so bear with me!

I am writing a code as follows:

number1 = input('Enter the first number: ')
number1 = int(number1)
number2 = input('Enter the second number: ')
number2 = int(number2)
number3 = input('Enter the third number: ')
number3 = int(number3)
ratio12 = int(number1 / number2)
ratio13 = int(number1 / number3)
ratio23 = int(number2 / number3)
print('The ratio of', + number1, '+', + number2,'is', + ratio12, '.')
print('The ratio of', + number1, '+', + number3,'is', + ratio13, '.')
print('The ratio of', + number2, '+', + number3,'is', + ratio23, '.')

The code is functional (finally), but I can't seem to get rid of the space before the period on the print statements. Is there a way that I can do that?

Upvotes: 0

Views: 10701

Answers (6)

Jamie C
Jamie C

Reputation: 463

The reason why this happens is because you are using commas in your print statements. In python there are a few ways to give the print statement multiple variables, you seem to be mixing two of them together. The ways are as follows.

  • Concatenate the string.
    print('The ratio of ' + str(number1) + ' + ' + str(number2) + ' is ' + str(ration12) + '.')
    This way is probably the most basic way. It will join the strings without adding any characters in between them (e.g. no spaces in between unless you add them explicitly.) Also note, that string concatenation won't automatically cast the integers to a string for you.
  • Pass print multiple arguments.
    print('The ratio of', number1, '+', number2, 'is', ration12, '.')
    This will automatically add spaces between each argument and is what is happening in your case. The separator (which defaults to a space) can be changed by passing a keyword argument to the print function. For example, print('i = ', i, sep='')
  • Use string formatting.
    print('The ratio of {} + {} is {}.'.format(number1, number2, ratio12))
    This way is the most readable and often the best way. It will replace the '{}' sections in you 'template' string with the arguments based into the format function. It does this in order, however you can add an index like this '{0}' to explicitly use an argument by index.

Upvotes: 3

Mike Müller
Mike Müller

Reputation: 85492

Some string formating makes your live easier:

number1 = 1
number2 = 2
ratio12 = number1 / number2
print('The ratio of {} + {} is {}.'.format(number1, number2, ratio12))

Output:

The ratio of 1 + 2 is 0.5.

Upvotes: 1

cadolphs
cadolphs

Reputation: 9647

That's the way print works when you give it multiple comma separated arguments. The logic behind that is that when you quickly want to print a bunch of variables, it's a pain to manually add widespace.

Well, one thing to try: Get rid of all the , in the print statement. You can just chain strings using the + sign.

So,

print('The ratio of ' + str(number1) + ' ' + str(number2) + ' is ' + str(ratio12) + '.')

If you need even greater control over formatting, you'd want to look into the format function.

print("The ratio of {} + {} is {}.".format(number1, number2, ratio12))

Upvotes: 0

Alex
Alex

Reputation: 779

Try to write to it this way:

print('The ratio of %d + %d is  %d.' %(number1, number2, ratio12))
print('The ratio of %d + %d is  %d.' %(number1, number3, ratio13))
print('The ratio of %d + %d is  %d.' %(number2, number3, ratio23))

Upvotes: 0

Prune
Prune

Reputation: 77850

You're confused about the concatenation function and print fields. If you're going to concatenate all those strings, just use concatenation. The comma includes the default separator.

print('The ratio of', number1, '+', number2,'is', str(ratio12) + '.')

Upvotes: 0

mgilson
mgilson

Reputation: 310069

You can control the "separator" using the sep argument to print:

print('The ratio of', + number1, '+', + number2,'is', + ratio12, '.', sep='')

Note that this will change the spacing between the other items as well.


Also -- You don't need the extra + operators in there. Here's a version without the spaces and with explicit spaces added where I think you want them:

print('The ratio of ', number1, ' + ', number2, ' is ', ratio12, '.', sep='')

Upvotes: 0

Related Questions