내 연락처 정보
우편메소피아@프로톤메일.com
2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
목차
문자열: 여러 문자로 구성된 문자열
문자: 육안으로 볼 수 있는 기호
바이트: 육안으로 보이지 않음
파이썬이 문자열을 정의하는 방법:
작은따옴표 큰따옴표 삼중따옴표 str()
>>> dir(s) ['capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
대문자 ------- 문자열의 첫 글자를 대문자로 만듭니다.
>>> s 'this is a string' >>> s.capitalize() 'This is a string'
center(width, fillchar=' ') ------ 주어진 길이에 따라 문자열을 중앙에 배치하고 중앙에 정렬합니다.
rjust --------- 오른쪽 정렬
그냥 ------ 왼쪽 정렬
>>> help(s.center) Help on built-in function center: center(width, fillchar=' ', /) method of builtins.str instance Return a centered string of length width. Padding is done using the specified fill character (default is a space). >>> s.center(40) ' this is a string ' >>> s.center(40,"*") '************this is a string************' >>> s.rjust(40) ' this is a string' >>> s.ljust(40) 'this is a string ' >>> s.ljust(40,"*") 'this is a string************************' >>> s.rjust(40,"*") '************************this is a string'
count --------- 문자열에 문자가 나타나는 횟수를 셉니다.
>>> s 'this is a string' >>> s.count("s") 3 >>> s.count("is") 2
endwith ------- 문자열이 XXX로 끝나는지 확인
startwith ------- 문자열이 XXX로 시작하는지 확인
>>> s.endswith("g") True >>> s.endswith("ing") True >>> s.endswith("f") False >>> s.startswith("t") True >>> s.startswith("f") False
index ------ 검색된 문자나 문자열이 문자열에 처음 나타나는 위치입니다. 존재하지 않는 경우 예외(오류 보고)가 발생합니다.
rindex ------- 오른쪽에서 왼쪽으로 검색, 검색되는 문자 또는 문자열이 문자열에서 처음 나타나는 위치(문자열에서 왼쪽에서 오른쪽으로 검색하는 문자 또는 문자열이 마지막으로 나타나는 위치와 동일) 위치 ), 존재하지 않는 경우 예외를 발생시킵니다(오류 보고).
>>> s 'this is a string' >>> s.index("s") 3 >>> s.index("is") 2 >>> s.index("f") Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: substring not found >>> s.index(" ") 4 >>> s.rindex("s") 10 >>> s.rindex("f") Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: substring not found
find ------- 문자열에서 검색 중인 문자 또는 문자열이 처음 나타나는 위치입니다. 존재하지 않는 경우 -1이 반환됩니다.
rfind----- 오른쪽에서 왼쪽으로 검색, 문자열에서 문자 또는 문자열이 처음 나타나는 위치(문자열 또는 문자열이 왼쪽에서 오른쪽으로 마지막으로 나타나는 위치와 동일)), 존재하지 않는 경우 -1을 반환합니다.
>>> s 'this is a string' >>> s.find("s") 3 >>> s.find("f") -1 >>> s.rfind("f") -1 >>> s.rfind("s") 10
encode ----- python3에서 문자열을 바이트로 변환하는 메서드, decode() 바이트를 문자열로 변환하는 메서드(바이트 단위의 메서드)
>>> s 'this is a string' >>> s.encode() b'this is a string' >>> d = s.encode() >>> d b'this is a string' >>> type(d) <class 'bytes'> >>> d b'this is a string' >>> d.decode() 'this is a string' >>> ss = d.decode() >>> ss 'this is a string' >>> type(ss) <class 'str'>
형식 ------ 형식 문자열, 문자열 연결
isupper ------ 문자열이 모두 대문자인지 확인합니다.
islower ----- ------ 문자열이 모두 소문자인지 확인
>>> s.isupper() False >>> s.islower() False
istitle ----- 제목인지 확인
>>> s.istitle() False >>> ss = "This Is A Dog" >>> ss.istitle() True
isspace ----- 공백인지 여부를 판별하는 데 일반적으로 사용되지 않습니다.
>>> sss = " " >>> sss.isspace() True >>> ss 'This Is A Dog' >>> ss.isspace() False
isdigit ------ 숫자인지 확인
>>> sss = "123234344" >>> sss.isdigit() True >>> ss 'This Is A Dog' >>> ss.isdigit() False >>> sss = "1233443gggg" >>> sss.isdigit() False
isalnum ------ 문자와 숫자(영숫자)인지 확인합니다.
>>> help(s.isalnum) Help on built-in function isalnum: isalnum() method of builtins.str instance Return True if the string is an alpha-numeric string, False otherwise. A string is alpha-numeric if all characters in the string are alpha-numeric and there is at least one character in the string. >>> sss '1233443gggg' >>> sss.isalnum() True >>> ss 'This Is A Dog' >>> ss.isalnum() False
isalpha ------ 문자인지 여부를 확인합니다.
제목 ----- 문자열을 제목 형식으로 변환
>>> s 'This is a string' >>> s.istitle() False >>> s.title() 'This Is A String' >>> sss = s.title() >>> sss 'This Is A String' >>> sss.istitle() True
upper ------- 문자열을 대문자로 변환
lower ------- 문자열을 소문자로 변환
>>> s 'This is a string' >>> s.lower() 'this is a string' >>> s.upper() 'THIS IS A STRING'
분할 ----- 지정된 기호에 따라 문자열을 분할하고 반환 값은 목록입니다.
>>> s 'This is a string' >>> s.split(" ") ['This', 'is', 'a', 'string'] >>> l = s.split(" ") >>> l ['This', 'is', 'a', 'string'] >>> type(l) <class 'list'> >>> l = s.split("s") >>> l ['Thi', ' i', ' a ', 'tring']
Join ------ 지정된 형식에 따라 반복 가능한 객체를 문자열로 연결합니다.
>>> ls = ["A","B","c","d"] >>> type(ls) <class 'list'> >>> ss = " ".join(ls) >>> ss 'A B c d' >>> type(ss) <class 'str'> >>> ss = "*".join(ls) >>> ss 'A*B*c*d'
스트립 ------- 양쪽 공백 제거
rstrip------ 오른쪽 공백을 비웁니다.
lstrip -------- 왼쪽 공백 지우기
>>> ss = " hhhhhhh " >>> ss ' hhhhhhh ' >>> ss.strip() 'hhhhhhh' >>> ss.strip() 'hhhhhhh' >>> ss ' hhhhhhh ' >>> ss.rstrip() ' hhhhhhh' >>> ss.lstrip() 'hhhhhhh '
바꾸기 ("원래 문자", "새 문자") ------ 해당 문자 바꾸기
>>> s 'This is a string' >>> s.replace("s","t") 'Thit it a ttring'
슬라이스: 반복 가능한 객체(컨테이너)를 분리하는 데 사용됩니다.
문법 규칙:
object[start_index:end_index:step]
start_index ------- 시작 인덱스(시작 위치)
end_index ------ 끝 인덱스(끝 위치), end_index의 값을 포함하지 않음
단계 ---- 단계 길이, 양수 또는 음수일 수 있음, 양수(왼쪽에서 오른쪽으로) 음수(오른쪽에서 왼쪽으로), 기본값은 1
object[:] ----- 切割的是一个完整的对象 object[start_index:] ------ 从start_index开始切割到最后的位置,步长为1 object[:end_index] ------ 从最开始的位置切割到end_index处,但是不包含end_index object[start_index:end_index] ------- 从start_index开始切割到end_index处,但是不包含end_index,步长为1 object[start_index:end_index:step] ------------ 从start_index开始切割到end_index处,但是不包含end_index,步长为step
앞면 마감과 뒷면 열기 사이의 간격
ls = [0,1,2,3,4,5,6,7,8,9]
1. 단일 값 잘라내기
>>> ls [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> ls[0] 0 >>> ls[-4] 6
2. 완전한 개체를 잘라냅니다
>>> ls[:] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> ls[::] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> ls[::-1] #-1 表示从右往左切割 所以是倒序 [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
3. start_index와 end_index는 모두 양수입니다.
>>> ls [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> ls[1:6] [1, 2, 3, 4, 5] >>> ls[1:6:-1] # start_index=1 end_index = 6表示的是从1切割到6的位置,但是不包含6处的值,从左往右切割,step=-1 表示从右往左 [] >>> ls[6:1] [] >>> ls[6:1:-1] [6, 5, 4, 3, 2] >>> ls[:6] [0, 1, 2, 3, 4, 5] >>> ls[:6:-1] [9, 8, 7] >>> ls[6:] [6, 7, 8, 9] >>> ls[6::-1] [6, 5, 4, 3, 2, 1, 0] >>>
4. start_index와 end_index는 모두 음수입니다.
>>> ls [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> ls[-1:-6] [] >>> ls[-1:-6:-1] [9, 8, 7, 6, 5] >>> ls[-6:-1] [4, 5, 6, 7, 8] >>> ls[:-6] [0, 1, 2, 3] >>> ls[:-6:-1] [9, 8, 7, 6, 5] >>> ls[-6:] [4, 5, 6, 7, 8, 9] >>> ls[-6::-1] [4, 3, 2, 1, 0]
5. Start_index와 end_index는 양수와 음수로 결합됩니다.
>>> ls [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> ls[1:-6] [1, 2, 3] >>> ls[1:-6:-1] [] >>> ls[-1:6] [] >>> ls[-1:6:-1] [9, 8, 7]
6. 연속 슬라이싱
>>> ls [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> ls[:8][2:5][-1:] [4] ls[:8] ----- [0,1,2,3,4,5,6,7] ls[:8][2:5] ----- [2,3,4] ls[:8][2:5][-1:] ----- [4]
7. 슬라이싱의 세 가지 매개변수는 표현식이 될 수도 있습니다.
>>> ls [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> ls[2+1:3*2:7%3] [3, 4, 5] >>> ls[3:6:1] [3, 4, 5] >>> ls[2:6:2] [2, 4]
8. 슬라이싱은 다른 물체도 자를 수 있습니다.
>>> (1,2,3,4,5,6,7,8,9)[:3] (1, 2, 3) >>> "ABCDEFGHIJK"[::2] 'ACEGIK' >>> for i in range(1,100)[2::3][-10:]: ... print(i) ... 72 75 78 81 84 87 90 93 96 99
인터뷰 질문들:
1. 슬라이스를 사용하여 데이터를 잘라낼 때 아래 첨자 범위를 초과하면 오류가 보고됩니까?
오류가 보고되지 않습니다. 범위를 초과하면 전체 개체가 반환됩니다. 일치하지 않으면 []가 반환됩니다.
2. Python에서 목록 출력을 반전시키는 방법은 무엇입니까?
루프 역방향
[::-1]
리스트.역방향()