Maron's DATA Log

[Python] 문자열 함수 본문

Python

[Python] 문자열 함수

maron2ee 2020. 11. 27. 15:30

# 슬라이싱

 

jmbh = "900123-1234567"

 

print("sex : " + jmbh[7])

 

# 인텍스 0부터 6직전까지 (0, 1, 2, 3, 4, 5)

print("생년월일 : " + jmbh[:6])     

 

# 7번째 부터 끝까지

print("뒤 7자리 : " + jmbh[7:])     

 

# 맨 뒤에서 7번째 부터 끝까지

print("뒤 7자리 : " + jmbh[-7:])   

 

 

* 문자열도 인덱싱과 슬라이싱을 이용할 수 있지만, 특정 인덱스의 값을 변경할 수는 없음

 

# 문자열 처리

 

hello = "Hello World"

 

# lower : 소문자

print(hello.lower( ))     

 

# upper : 대문자

print(hello.upper( ))    

 

# isupper : [ ] 번째 문자 대문자 인지

print(hello[0].isupper( ))     

 

# replace : 문자열 대체

print(hello.replace("Hello", "Hi))     

 

# index : 원하는 값 위치 탐색 1

index = hello.index("o")     

print(index)

 

index = hello.index("o", index + 1)

print(index)

 

 # find : 원하는 값 위치 탐색 2

print(hello.find("o"))    

 

hello = "Hello World"

 

print(hello.find("Hello"))

print(hello.find("Hi"))

print(hello.index("Hi"))

 

# count : 문자 개수 세기

count = hello.count("o")     

2

 

 

 

# 문자열 포멧

 

print("a" + "b")     # '+' 를 이용해 연결 (Concatenate)

print("a", "b")

 

1.

print("나는 %s을 좋아해요." % "파이썬")

 

2.

print("나는 {age}살이며, {address}에 살아요.".format(age=20, address="서울"))

 

3. 

age = 20

address = "서울"

print(f"나는 {age}살이며, {address}에 살아요.")

 

 

# \n : 줄바꿈

print("백문이 불여일견\n백견이 불여일타")

 

# \".   \".  &  \'    \'   : 큰 따옴표나 작은 따옴표를 원하는 만큼 포함 시킬 수 있음 

print("저는 \"maron\"입니다.")

print("저는 \'maron\'입니다.")

 

# \\ : 문장 내에서 \

 

# \r : 커서를 맨 앞으로 이동

print("Red Apple\rPine")

 

# \b : 백스페이스 (한 글자 삭제)

print("Pineed\bApple")

 

# \t : 탭

print("Pine\tApple")

 

Comments