Python - Ch2

euNung·2022년 6월 26일
0

Python

목록 보기
2/2
  • 인덱싱
    : 해당 인덱스의 값을 찾음

  • 슬라이싱
    : 시작 인덱스부터 (마지막 인덱스 - 1) 값까지 잘라냄
apple = 'apple'

# 인덱싱
index1 = apple[1]
print(index1)    		# 'a'

# 슬라이싱
slice1 = apple[2:]
print(slice1)			# 'ple'
  • 문자열 정렬
# 오른쪽 정렬
"%10s" % ("apple")		# '     apple'

# 왼쪽 정렬
"%-10s" % ("apple")		# 'apple     '
  • 문자열 분리 (문자열 => 리스트)
    split('분리할 문자열')
str = 'apple, 2, green, 5'
new_str = str.split(',')		# ['apple', '2', 'grren', '5']
  • 문자열 합침(리스트 => 문자열, 문자열 => 문자열)
lst = ['K', 'O', 'R', 'E', 'A']
new_str = "".join(lst)		# "KOREA"

str = "KOREA"
new_str = ",".join(str)  	# "K,O,R,E,A"
  • 문자열 위치 찾기
str = "KOREA"
str.find('R')				# 2
profile
프론트엔드 개발자

0개의 댓글