์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- C
- ํ์ด์ฌ ์๊ณ ๋ฆฌ์ฆ
- ๋ฌ์คํธ ์์
- ์๋ฐ ๊ฐ๋
- ์ด์์ฒด์
- ๋ฌ์คํธ
- ๋ฐ์ดํฐ ํต์
- ์ฐ๋ถํฌ
- ์ค๋ผํดDB
- ํ์ด์ฌ
- ๋ฐฑ์ค ๋ฌ์คํธ
- Reversing
- ์๋ฐ ๊ธฐ์ด
- ์๋ฐ
- Python challenge
- ubuntu
- OS
- ํ์ด์ฌ ์ฑ๋ฆฐ์ง
- java
- Rust
- ๋ฐฑ์ค
- ์ค๋ผํด
- ์๊ณ ๋ฆฌ์ฆ
- data communication
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- Database
- ๋ฌ์คํธ ํ๋ก๊ทธ๋๋ฐ ๊ณต์ ๊ฐ์ด๋
- Operating System
- ํ์ด์ฌ ์ฒผ๋ฆฐ์ง
- Python
Archives
- Today
- Total
IT’s Portfolio
[Python] ํ์ด์ฌ์ ๋ฌธ์์ด ํฌ๋ฉํ Ver.2 ๋ณธ๋ฌธ
Development Study/Python
[Python] ํ์ด์ฌ์ ๋ฌธ์์ด ํฌ๋ฉํ Ver.2
f1r3_r41n 2022. 10. 6. 14:08728x90
๋ฐ์ํ
๐ป ํ์ด์ฌ ๋ฌธ์์ด ํฌ๋ฉํ Ver.2
https://it-neicebee.tistory.com/102
๋ด์ฉ์ด ๋ถ์คํ์ฌ ๊ณต๋ถํ ๊ฒธ ์ถ๊ฐ๋ก ๋ ์์ฑํจ
%
- ๊ตฌ ๋ฐฉ์
- ๊ธธ์ด์ง๋ฉด ๊ฐ๋ ์ฑ ๋จ์ด์ง
- ํ์ด์ฌ ๊ณต์ ๋ฌธ์์์ ๊ถ์ฅํ์ง ์์
- C ์ธ์ด์ ๋ณ์ ํ์ ์ง์ ๊ณผ ๋น์ท
msg = "I like gnuykob"
p1 = 3.141592653589793238462643383
print("%25s" % msg)
print("%-25s" % msg)
print("%5s" % msg) # ๊ณต๋ฐฑ๋ณด๋ค ๋ฌธ์์ด์ด ํฌ๋ฉด ์ค๋ฒํ๋ก์ฐ
print("%.5s" % msg)
print("%10.5s" % msg)
print("%f" % p1)
print("%10.5f" % p1)
print("%-10.5f" % p1)
- ๊ณต๋ฐฑ์ด ํํํ๊ณ ์ ํ๋ ๋ฐ์ดํฐ๋ณด๋ค ํด ๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์ค๋ฅธ์ชฝ ์ ๋ ฌ
- ์ผ์ชฝ ์ ๋ ฌ์
-
๊ธฐํธ๋ฅผ ์ฌ์ฉ - ์์์ ์ ๊ธฐ๋ณธ ์ถ๋ ฅ์ 7๋ฒ์งธ ์๋ฅผ ๋ฐ์ฌ๋ฆผํ์ฌ 6๋ฒ์งธ ์๋ฆฌ๊น์ง ์ถ๋ ฅํจ
- Result
-
I like gnuykob I like gnuykob I like gnuykob I lik I lik 3.141593 3.14159 3.14159
-
format()
- Python 3 ํฌ๋ฉง ๋ฐฉ์
- ๊ธธ์ด์ง๋ฉด ๊ฐ๋ ์ฑ์ด ๋จ์ด์ง
msg1 = "I"
msg2 = "like"
msg3 = "gnuykob"
p1 = 3.141592653589793238462643383
# ์๋ ๋๋ฒ๋ง
# ๋๋ฒ๋ง ์ํ๋ฉด format์ ์ธ์๊ฐ์ ์์๋๋ก ์ฝ์
# ์ธ์๊ฐ๋ณด๋ค ๋ง์ {} ์ฌ์ฉ -> Error
# {} ๋ณด๋ค ์ ์ ์ธ์๊ฐ -> Error
print("{} {} {}".format(msg1, msg2, msg3))
# ๋๋ฒ๋ง
# ๋๋ฒ๋ง์ ํ๋ค๊ฐ ์ค๊ฐ์ ์ ํ ๊ฒฝ์ฐ -> Error
print("{1} / {0} / {2}".format(msg1, msg2, msg3))
# * ๊ธฐํธ๋ก ์ธ์ ์ธํจํน
print("{} / {} / {}".format(*msg3))
# ์ด๋ฆ ์ง์ ์ฌ์ฉ
print("{me} & {you}".format(me=msg1, you=msg3))
# dict ์๋ฃํ ์ฌ์ฉ
my_dict = {'name': 'f1r3_r41n', \
'job': 'developer', 'age': '22'}
print("{name} {age} {job}".format(**my_dict))
# list ์๋ฃํ ์ฌ์ฉ
my_list = ['f1r3_r41n', 'developer', '22']
print("{1} / {0} / {0} / {2}".format(*my_list))
# dict & list
my_dili = [{'name': 'f1r3_r41n', \
'job': 'developer', 'age': '22'}, \
{'name': 'gnuykob', 'job': 'data engineer', \
'age': '22'}]
print("{0[name]} {0[age]} {0[job]}".format(*my_dili))
print("{1[name]} {1[age]} {1[job]}".format(*my_dili))
# ๊ธฐ๋ณธ ์ผ์ชฝ ์ ๋ ฌ
# < ์ผ์ชฝ ์ ๋ ฌ, > ์ค๋ฅธ์ชฝ ์ ๋ ฌ, ^ ๊ฐ์ด๋ฐ ์ ๋ ฌ
print("{:<11}".format(msg3))
print("{:>11}".format(msg3))
print("{:^11}".format(msg3))
# - ๊ธฐํธ๋ก ๊ณต๋ฐฑ ์ฑ์
print("{:-^11}".format(msg3))
# ๋ณตํฉ
print("{0:-^{1}}".format(msg3, 22))
# ์ซ์ ๋ถํธ ํํ (์์์ ์ดํ ๋์ผ)
print("{:+d} / {:+d}".format(+21, -21)) # + ํญ์ ๋ถํธ ํ์
print("{:-d} / {:-d}".format(+21, -21)) # - ์์์ผ ๊ฒฝ์ฐ ๋ถํธ ํ์
print("{:=+5d} / {:=+5d}".format(+21, -21)) # ๊ธฐํธ๋ง ์์ผ๋ก ์ ๋ ฌ
print("{:+05d} / {:+05d}".format(+21, -21)) # ๊ณต๋ฐฑ 0์ผ๋ก ์ฑ์
# ์ง์ ํ๊ธฐ
print("{:e}".format(p1))
print("{:E}".format(p1))
# ๋ฐฑ๋ถ์จ ํ๊ธฐ
print("{0} / {0:%}".format(45/100))
# ์๋ฏธ์๋ ์์์ ์ญ์
print("{}".format(3.000000)) # 0 ํ์
print("{:g}".format(3.000000)) # 0 ์ญ์
# ์ฒ๋จ์ ๊ตฌ๋ถ
print("{:,}".format(30000000))
print("{:,.2f}".format(30000000.1234))
# ์ง๋ฒ
print("{0:d} / {0:x} / {0:o} / {0:b}".format(79)) # dec, hex, oct, bin
print("{0:#d} / {0:#x} / {0:#o} / {0:#b}".format(79)) # ์ ๋์ด ํ์
# hex์์ ๋์๋ฌธ์ ๊ตฌ๋ถ์ x, X ์ฌ์ฉ
# ์ ๋์ฝ๋
print("{0:c}{1:c}{2:c}".format(98, 101, 101))
# ๊ฐ ์ค๊ดํธ ๋ ๋ฒ ์ฌ์ฉํ์ฌ ์ค๊ดํธ ํ๊ธฐ
print("{{ {0:c}{1:c}{2:c} }}".format(98, 101, 101))
- Result
-
I like gnuykob like / I / gnuykob g / n / u I & gnuykob f1r3_r41n 22 developer developer / f1r3_r41n / f1r3_r41n / 22 f1r3_r41n 22 developer gnuykob 22 data engineer gnuykob gnuykob gnuykob --gnuykob-- -------gnuykob-------- +21 / -21 21 / -21 + 21 / - 21 +0021 / -0021 3.141593e+00 3.141593E+00 0.45 / 45.000000% 3.0 3 30,000,000 30,000,000.12 79 / 4f / 117 / 1001111 79 / 0x4f / 0o117 / 0b1001111 bee { bee }
-
F-String
- Python 3.6 ์ด์ ๋ฒ์ ์์ ์ง์
- ์ฝ๋๊ฐ ๊ธธ์ด์ ธ๋ ์ง๊ด์ ์ผ๋ก ํ์ ๊ฐ๋ฅ
- ๋ณดํธ์ ์ธ "ํ์ด์ฌ์ค๋ฌ์ด" ๋ฐฉ์
format()
๋ฐฉ์๊ณผ ๋ค๋ฅด๊ฒ ๋ณ์๋ช ์ ์ง์ ํ ๋นํ๋ ๊ฒ์ด ์ฐจ์ด์
msg1 = "I"
msg2 = "like"
msg3 = "gnuykob"
p1 = 3.141592653589793238462643383
print(f"{msg1} {msg2} {msg3}")
# dict ์๋ฃํ ์ฌ์ฉ
my_dict = {'name': 'f1r3_r41n', \
'job': 'developer', 'age': '22'}
print(f"He's name: {my_dict['name']}")
# list ์๋ฃํ ์ฌ์ฉ
my_list = ['f1r3_r41n', 'developer', '22']
print(f"He's age: {my_list[2]}")
# dict & list
my_dili = [{'name': 'f1r3_r41n', \
'job': 'developer', 'age': '22'}, \
{'name': 'gnuykob', 'job': 'data engineer', \
'age': '22'}]
print(f"He's name: {my_dili[0]['name']}")
print(f"She's name: {my_dili[1]['name']}")
# ๊ธฐ๋ณธ ์ผ์ชฝ ์ ๋ ฌ
# < ์ผ์ชฝ ์ ๋ ฌ, > ์ค๋ฅธ์ชฝ ์ ๋ ฌ, ^ ๊ฐ์ด๋ฐ ์ ๋ ฌ
print(f"{msg3:<11}")
print(f"{msg3:>11}")
print(f"{msg3:^11}")
# - ๊ธฐํธ๋ก ๊ณต๋ฐฑ ์ฑ์
print(f"{msg3:-^11}")
# ๋ณตํฉ
print(f"{msg3:-^{22}}")
# ์ซ์ ๋ถํธ ํํ (์์์ ์ดํ ๋์ผ)
p_num = 21
m_num = -21
print(f"{p_num:+d} / {m_num:+d}") # + ํญ์ ๋ถํธ ํ์
print(f"{p_num:-d} / {m_num:-d}") # - ์์์ผ ๊ฒฝ์ฐ ๋ถํธ ํ์
print(f"{p_num:=+5d} / {m_num:=+5d}") # ๊ธฐํธ๋ง ์์ผ๋ก ์ ๋ ฌ
print(f"{p_num:+05d} / {m_num:+05d}") # ๊ณต๋ฐฑ 0์ผ๋ก ์ฑ์
# ์ง์ ํ๊ธฐ
print(f"{p1:e}")
print(f"{p1:E}")
# ๋ฐฑ๋ถ์จ ํ๊ธฐ
print(f"{45/100:%}")
# ์๋ฏธ์๋ ์์์ ์ญ์
num3 = 3.000000
print(f"{num3}") # 0 ํ์
print(f"{num3:g}") # 0 ์ญ์
# ์ฒ๋จ์ ๊ตฌ๋ถ
num4 = 3000000000
num5 = 3000000000.1234
print(f"{num4:,}")
print(f"{num5:,.2f}")
# ์ง๋ฒ
num6 = 79
print(f"{num6:d} / {num6:x} / {num6:o} / {num6:b}") # dec, hex, oct, bin
print(f"{num6:#d} / {num6:#x} / {num6:#o} / {num6:#b}") # ์ ๋์ด ํ์
# hex์์ ๋์๋ฌธ์ ๊ตฌ๋ถ์ x, X ์ฌ์ฉ
# ์ ๋์ฝ๋
print(f"{98:c}{101:c}{101:c}")
# ๊ฐ ์ค๊ดํธ ๋ ๋ฒ ์ฌ์ฉํ์ฌ ์ค๊ดํธ ํ๊ธฐ
print(f"{{ {98:c}{101:c}{101:c} }}")
- Result
-
I like gnuykob He's name: f1r3_r41n He's age: 22 He's name: f1r3_r41n She's name: gnuykob gnuykob gnuykob gnuykob --gnuykob-- -------gnuykob-------- +21 / -21 21 / -21 + 21 / - 21 +0021 / -0021 3.141593e+00 3.141593E+00 45.000000% 3.0 3 3,000,000,000 3,000,000,000.12 79 / 4f / 117 / 1001111 79 / 0x4f / 0o117 / 0b1001111 bee { bee }
-
728x90
๋ฐ์ํ
'Development Study > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python] requests์ urllib (0) | 2022.11.18 |
---|---|
[Algorithm] Baekjoon - ๋ฐ๋ณต๋ฌธ ๋จ๊ณ (0) | 2022.10.12 |
[Python] ๋นํธ ์ฐ์ฐ์ (0) | 2022.10.01 |
[Python] eval(), exec()์ ๋ํด์ (0) | 2022.08.16 |
[Algorithm] Baekjoon - ์กฐ๊ฑด๋ฌธ ๋จ๊ณ (0) | 2022.08.05 |
Comments