관리 메뉴

Jun Hyuk Kim's Blog

About printing string formats in python 본문

Coding Explanation

About printing string formats in python

junhyuk1229 2023. 3. 8. 11:11

There are 3 different ways to print a string using a format.

 

First, is using the '%' symbol like is the c language.

 

Variables can be printed with their corresponding types (int = %d, float = %f, string = %s)

Input:

a = 1
b = 1.23
c = 'hello'
print("%d %f %s" % (a, b, c))

Output:

1 1.230000 hello

 

Floats can be printed with the wanted amount of decimal places.

Input:

b = 1.23
print("%.1f %.2f %.3f" % (b, b, b))

Output:

1.2 1.23 1.230

 

Extra backspaces can be printed with the following code

Input:

a = 1
b = 1.23
c = 'hello'
print("%10d" % a)
print("%7.2f" % b)
print("%10s" % c)

Output:

      1
1.23
  hello

The number in the front is the amount of spaces allocated for the output.

For example the first print line and the third print line have the same '10' in the front, so the 1 and 'o' in 'hello' are alligned.

 

When printing a float, if only a number is infront it will read that as the number of wanted decimal places. So a period is needed to seperate the two numbers. If only a number followed by a period is used, the float will be printed as an int.

Input:

b = 1.23
print("%4f" % b)
print("%7.f" % b)

Output:

1.230000
      1

 

For all the other formats, all the other parts are similar to to the code shown above so I will just add the code showing what it does with comments. If additional explanation is needed I will add it at the end.

 

The other way is to use the format extender.

Input:

a = 13
b = 2.583
c = "Hello"
# Normal print
print("{0}, {1}, {2}".format(a, b, c))
# Can change order
print("{1}, {0}, {2}".format(a, b, c))
# Float manipulation
print("{0:.1f}, {0:.2f}, {0:.3f}".format(b))
# Backspace manipulation
print("{0:<10s}".format(c))
print("{0:>10s}".format(c))
print("{0:^10s}".format(c))
# Use other character as backspace
print("{0:*<10s}".format(c))
print("{0:&>10s}".format(c))
print("{0:$^10s}".format(c))

Output:

13, 2.583, Hello
2.583, 13, Hello
2.6, 2.58, 2.583
Hello     
     Hello
  Hello   
Hello*****
&&&&&Hello
$$Hello$$$

In this format a direction symbol like '<', '^', '>' can be used to make the output allign to different positions in the output string. Also the characters that will the whitespaces can be changed to other symbols.

 

The last way, and the way that I will use for all future code, is using the fstring

Input:

a = 13
b = 2.583
c = "Hello"
# Normal print
print(f"{a}, {b}, {c}")
# Can change order
print(f"{b}, {a}, {c}")
# Float manipulation
print(f"{b:.1f}, {b:.2f}, {b:.3f}")
# Backspace manipulation
print(f"{c:<10s}")
print(f"{c:>10s}")
print(f"{c:^10s}")
# Use other character as backspace
print(f"{c:*<10s}")
print(f"{c:&>10s}")
print(f"{c:$^10s}")

Output:

13, 2.583, Hello
2.583, 13, Hello
2.6, 2.58, 2.583
Hello     
     Hello
  Hello   
Hello*****
&&&&&Hello
$$Hello$$$

It is completly the same as the above code but with less typing most of the times.

'Coding Explanation' 카테고리의 다른 글

[SSH] Transfering files between servers  (0) 2023.08.07
[Backend] CORS  (0) 2023.08.06
About testing and printing  (0) 2022.12.20