Home > Enterprise >  How can I get same result as I am thinking
How can I get same result as I am thinking

Time:05-05

year = "2020"
int_year = int(year)
print(int_year, '\n', int_year 1, '\n', int_year 2, '\n')

I wrote this code and I ran it and this is the result:

2020 
 2021 
 2022 

I want to make this to this:

2020 
2021 
2022

How can I make this?

CodePudding user response:

sparating the strings by comma inside the print will add spaces

what you can do is either append the strings with

print(str(int_year)   '\n'   str(int_year 1)   '\n'   str(int_year 2)   '\n')

or even better, use f-strings

print(f"{int_year}\n{int_year 1}\n{int_year 2}\n")

CodePudding user response:

This is the print function in python:

print(object(s), sep=separator, end=end, file=file, flush=flush)

You can see it takes an argument sep, it is how you want to separate your object into your print functions, by default it's "". You can use sep='\n' in your case.

CodePudding user response:

year = "2020"
int_year = int(year)
[[print(int_year i)] for i in range(3)]

using a list comprehension for any amount of iterations you want, you can just edit the range

  • Related