Python 문자열

sunj - May 9 - - Dev Community
#문자열 인덱스

text = 'abc'
print(text[0]) //a
print(text[1]) //b
print(text[2]) //c

print(text[-3]) //a
print(text[-2]) //b
print(text[-1]) //c
//인덱스는 float는 안된다, 정수만 가능
Enter fullscreen mode Exit fullscreen mode
#문자열 슬라이스

text='abcde fgh ijk'
text[2:5] // cde(마지막 인덱스+1)
text[1:8] // bcde fg(공백도 계산되어 출력)
text[-5:-1] // h ij

text[5:] // fgh ijk
// 5부터 끝까지
text[:5] //abcde
// 처음부터 5앞까지
Enter fullscreen mode Exit fullscreen mode
text[:]
//처음부터 끝까지
Enter fullscreen mode Exit fullscreen mode
text[2:8:2] //acef
//마지막은 글자간의 간격으로 출력
text[1:8:2] //bd g
text[1:8:-1] //범위가 맞지 않아 아무것도 출력안됨
text[8:0:-1] //hgf edcb
//두 번째 인덱스-1
text[::-1]//kji hgf edcba
Enter fullscreen mode Exit fullscreen mode
#출력 지정 : format(a,b,c,...)
text = 'abcde {} {}'
print(text.format('ABC',123))// abcde ABC 123 
//안맞을 땐 에러를 내거나 잘려서 나온다
Enter fullscreen mode Exit fullscreen mode
#대체하기 : replace(a,b)
text = 'abcde ABC ABC'
print(text.replace('ABC','KKK')) //abcde KKK KKK
Enter fullscreen mode Exit fullscreen mode
#자르기 : split(a)
text = 'abcde A/B/C A.B.C'

a,b,c=text.split() //공백을 기준으로 자름
print(a) //abcde
print(b) //A/B/C
print(c) //A.B.C


a,b,c=text.split('.') //.을 기준으로 자름
print(a) //abcde A/B/C A
print(b) //B
print(c) //C
Enter fullscreen mode Exit fullscreen mode
#합치기 : a.join()
text= 'abcde'
print('/'.join(text)) //a/b/c/d/e
Enter fullscreen mode Exit fullscreen mode
#개수 확인하기 : count(a)
text= 'abcde ABC ABC'
print(text.count('a')) //1
print(text.count('A')) //2
print(text.count('1')) //0
Enter fullscreen mode Exit fullscreen mode
#제거하기 : strip(a), lstrip(a), rstrip(a)
text = '   abcde   '
print(text.strip()) //abcde
//양쪽 제거
print(text.lstrip()) //abcde
//왼쪽만 제거
print(text.rstrip()) //   abcde
//오른쪽만 제거

text = '****ab*cde****'
print(text.strip('*')) //ab*cde
print(text.lstrip('*')) //ab*cde****
print(text.rstrip('*')) //****ab*cde

//중간은 제거해주지 못한다
Enter fullscreen mode Exit fullscreen mode
#인덱스 찾기 : find(a)/rfind(a)/index(a)/rindex(a)
text = 'ABC ABC'
print(text.find('A')) //0
print(text.rfind('A')) //4
//오른쪽에서부터 찾기
print(text.index('A'))  //0
print(text.rindex('A'))  //4
//오른쪽에서부터 찾기

print(text.find('d')) //-1
print(text.rfind('d')) //-1
//없는거 찾으라고할때
print(text.index('A'))  
print(text.rindex('A'))  
//에러를 리턴
Enter fullscreen mode Exit fullscreen mode
#확인하기 : isapha() / isdigit() / isalnum() / isupper()/ islower()

text1 = 'ABCabc123'
text2 = '123'
text3 = 'ABC'
text4 = 'abc'

print(text1.isalpha()) //False
print(text1.isdigit()) //False
print(text1.isalnum()) //True
//알파벳 또는 숫자의 조합인가
print(text1.isupper()) //False
print(text1.islower()) //False

print(text2.isalpha()) //False
print(text2.isdigit()) //True
//숫자의 조합인가
print(text2.isalnum()) //True
//알파벳 또는 숫자의 조합인가
print(text2.isupper()) //False
print(text2.islower()) //False

print(text3.isalpha()) //True
//알파벳의 조합인가
print(text3.isdigit()) //False
print(text3.isalnum()) //True
//알파벳 또는 숫자의 조합인가
print(text3.isupper()) //True
//대문자의 조합인가
print(text3.islower()) //False

print(text4.isalpha()) //True
//알파벳의 조합인가
print(text4.isdigit()) //False
print(text4.isalnum()) //True
//알파벳 또는 숫자의 조합인가
print(text4.isupper()) //False
print(text4.islower()) //True
//소문자의 조합인가
Enter fullscreen mode Exit fullscreen mode
#대/소문자 만들기 : upper()/lower()
text = 'ABCabc'
print(text.upper()) //ABCABC
print(text.lower()) //abcabc
Enter fullscreen mode Exit fullscreen mode
#0을 채우기 : zfill()
y='2020'
m='3'
d='1'

print(y.zfill(4)) //2020
print(y.zfill(2)) //03
print(y.zfill(2)) //01
Enter fullscreen mode Exit fullscreen mode

참조: 유투브 소놀코딩

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .