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.
Answer from Jamie C on Stack Overflow
Top answer
1 of 6
3

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.
2 of 6
1

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.
🌐
Reddit
reddit.com › r/learnpython › how can i add a period at the end of a print statement without having a space in-between an integer?
r/learnpython on Reddit: How can I add a period at the end of a print statement without having a space in-between an integer?
August 2, 2021 -

This is what I have written so far:

length = len(input("What is your name?\n"))

print("The length of your name is ",length,".")

Now, this is my output:

    What is your name?
    Shary
    The length of your name is  5 .

I would like my output to be like the following: "The length of your name is 5."

As you can imagine, by placing a comma next to the Integer length, I have an extra space I would like to take care of. I am new to Python so I do not really know how to solve this.

Any help would be greatly appreciated.

🌐
Machinelearninghelp
machinelearninghelp.org › programming-for-machine-learning › how-to-add-a-period-in-python-without-a-space
Mastering Python String Formatting - A Step-by-Step Guide to Adding a Period Without a Space
In the realm of advanced Python programming and machine learning, string formatting is an essential skill that can save you time and headaches. This article will guide you through adding a period at the end of a sentence without leaving unnecessary spaces using Python’s built-in string methods.
🌐
Quora
quora.com › How-do-I-insert-a-period-after-a-string-in-a-variable-e-g-print-Hello-name-I-want-the-period-after-name
How to insert a period after a string in a variable (e.g. print 'Hello',name - I want the period after name) - Quora
Answer (1 of 3): Just to give you another option: you can also write print "Hello, "+name+"." too, but the method suggested by Christian Pietsch (i.e. string interpolation) is actually my preferred way. Once you learn the syntax, you can interpolate all kinds of stuff into the string.
🌐
YouTube
youtube.com › codeflare
first line should end with a period python - YouTube
Download this code from https://codegive.com Title: Writing Pythonic Code: Properly Ending the First Line with a PeriodIntroduction:In Python, writing clean ...
Published   December 25, 2023
Views   72
Find elsewhere
🌐
Quora
quora.com › As-far-as-Python-goes-I-need-to-know-where-to-put-periods-and-commas-I-ll-worry-about-later
As far as Python goes I need to know where to put periods and commas? I’ll worry about {} later. - Quora
Answer (1 of 4): You use commas to separate parameters in a function call, or in the function header. Also, to separate return values, if there is more than one. Also, to separate items in a list, dictionary, etc. Examples: [code]def my_function(a, b): return a * b, a + b x, y = my_function...
🌐
Lenovo
lenovo.com › home
Periods in Computing: Functions and Uses | Lenovo US
For instance, =SUBSTITUTE(TRIM(A1), ... string without any spaces. A period-increment operator (.) in programming is used to increment a variable's value by one unit after each execution of a loop or iteration. It's commonly used with arrays or lists where data needs to be accessed sequentially.
🌐
Machinelearninghelp
machinelearninghelp.org › programming-for-machine-learning › how-to-add-a-period-after-a-variable-in-python
Mastering Pythonic Syntax
# Without a period x = 5 + 10 * 2 # With a period x = 5 . + 10 . * 2 · To implement this in your Python projects, follow these steps: Install the Black Linter: If you haven’t already, install Black, the uncompromising code formatter. ... Understand Black Configuration: Adjusting the configuration to include adding a period after variables is straightforward and can be found in the .style-guide file within your project directory.
🌐
Sublime Forum
forum.sublimetext.com › t › pressing-the-space-bar-twice-adds-periods-to-my-variable-names-inside-my-python-code › 70597
Pressing the `space-bar` twice adds periods to my variable names inside my python code - Technical Support - Sublime Forum
December 15, 2023 - Hey there, I just noticed a strange phenomena, pressing the space-bar twice automatically adds a period to my variable names inside my python code. How do I disable this? Thanks!
🌐
Reddit
reddit.com › r/python › and here i had just finished re-training myself to use one space after a period.
r/Python on Reddit: And here I had just finished re-training myself to use one space after a period.
April 6, 2011 - So the only reason you might ever need to actually type two spaces is in things like python comments, assuming you want them · I've no idea how good word is with ambiguous arrangements like your other replier described; offhand I'd expect it to be worse than LaTeX ;) More replies ... Actually it's just the opposite. A monospaced font's letters and characters each occupy the same amount of horizontal space. So a '. ' has enough reserved space on the right to make it readable without adding a second space.