Input/python

[python] 파이썬 공식홈페이지 string constants : ascii_letters, digits, punctuation

buji-learn 2023. 4. 17. 23:00

여태 필요하면 구글에 검색해서 사용하다가 파이썬 공식 문서를 보면서 정리해봤다. 역시 공식 홈페이지에는 뭐가 많다.

String constants (문자열 상수)

string.ascii_letters              : 소문자 a ~ z + 대문자 A ~ Z
string.ascii_lowercase        : 소문자 a ~ z
string.ascii_uppercase       : 대문자 A ~ Z
string.digits                         : 숫자 0 ~ 9
string.hexdigits                   : 16진법, 0 ~ 9 + a ~ f + A ~ F
string.octdigits                   : 8진법, 0~7
string.punctuation             : 특수문자
string.printable                  : 숫자 + 대소문자 + 특수문자 + 공백
string.whitespace              : 공백

 

# 알고리즘 문제에서 자주 쓰이는 문자열 상수

letters = string.ascii_letters 
# 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

lowercase = string.ascii_lowercase
# 'abcdefghijklmnopqrstuvwxyz'

uppercase = string.ascii_uppercase 
# 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

digits = string.digits
# '0123456789'

punctuation = string.punctuation
# '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
# 알아두기

hexdigits = string.hexdigits
# '0123456789abcdefABCDEF'

octdigits = string.octdigits
# '01234567'

printable = string.printable
# '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'

whitespace = string.whitespace
# ' \t\n\r\x0b\x0c'
# whitespace 차이

print('characters space : ab cd')
print('tab : ab\tcd')
print('linefeed : ab\ncd')
print('--------')
print('return : ab\rcd')
print('--------')
print('formfeed : ab\vcd')
print('formfeed2 : ab\x0bcd')
print('vertical tab : ab\fcd')
print('vertical tab2 : ab\x0ccd')
characters space : ab cd
tab : ab	cd
linefeed : ab
cd
--------
cd
--------
formfeed : abcd
formfeed2 : abcd
vertical tab : abcd
vertical tab2 : abcd

 

파이썬 3.11/Lib/string.py

 

GitHub - python/cpython: The Python programming language

The Python programming language. Contribute to python/cpython development by creating an account on GitHub.

github.com

https://docs.python.org : string constants 

 

string — Common string operations

Source code: Lib/string.py String constants: The constants defined in this module are: Custom String Formatting: The built-in string class provides the ability to do complex variable substitutions ...

docs.python.org

 

항상 알고나면 간단해지는 것 같다.