[python] Assigning a dynamic left space in a print statement, eliminating \t when writing on the next line

Asked 2 years ago, Updated 2 years ago, 113 views

Hello.

When I printed out Python print, two things were inconvenient for a long time, but it wasn't fatal, but there were parts that kept bothering me, so I'm asking you a question!

Question 1. Dynamically Assigning Left Spaces \nOr can I get a blank value as much as "February 14th (Mon): " of the previously printed values in the next print?

Currently, we divide it into two print statements and give the rjust arbitrary value to the output state below, check the print, and change the value, but when I changed the font once, the margin is twisted again.

a = 'February 14 (Mon): '

The values of len(a) and rjust don't match.

Is there a good way to print it out neatly by aligning the upper and lower positions like below? It would be nice to have an example of one print statement (\n) and two print statements, but if you don't, please share the knowledge you know!

# output
February 14 (Mon): (AA type) abcdef
              (Type B) abcdef

Question 2. If I type in the print door, it automatically enters \t, so can I make sure it doesn't If you enter and print it out in a good way on the code like the code below, \t is automatically attached as much as it is indented, but is there a way to keep the indentation in the code and remove it when it is printed?

a = 'a'
if a:
    print('''
    Hello.
    Hello.
    ''')
# Current output
    Hello.
    Hello.

# The output you want
Hello.
Hello.

python print

2022-09-20 11:13

1 Answers

print('''
    \nHello.
    \nHello.
    ''')
Or
print('\nHello.\nHello.')

In the case of spaces... Doesn't it come out differently from console to console? Why don't we do it this way?

print('abcd\n\tbcd\n\tcdf')
>>
abcd
    bcd
    cdf


2022-09-20 11:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.