파이썬 기초 | 슬라이싱 | format() | f-string | replace() | strip() | startswith(), endswith()

728x90

 

 

▶ .format(  ,  ) 

 

name1 = '김민수'
age1 = 10
name2 = '이철희'
age2 = 13

print('이름: {} , 나이: {}'.format(name1,age1))
print('이름: {} , 나이: {}'.format(name2,age2))

 


▶ f - string

 

name1 = '김민수'
age1 = 10
name2 = '이철희'
age2 = 13

print(f'이름: {name1}, 나이: {age1}')
print(f'이름: {name2}, 나이: {age2}')

 


▶ .replace("바꿀 것", "바뀔 것")

 

상장주식수 = '5,969,792,550'
컴마제거 = 상장주식수.replace(',',"")
타입변환 = int(컴마제거)
print(타입변환, type(타입변환))

 


▶ .strip()  

: 공백 없애기

 

data = "    삼성전자    "
print(data)
data1 = data.strip()
print(data1)

 

* .rstrip() : 오른쪽 공백 제거

* .lstrip() : 왼쪽 공백 제거

 


▶ .capitalize()

: 단어의 맨 앞 알파벳을 대문자로 바꾸기

 

a = 'hello'
a = a.capitalize()
print(a)

 


▶ startswith("찾는단어")  /  .endswith("찾는단어") 

: 찾는 단어가 있으면 True 반환

 

* .startswith()

file_name = '2020_보고서.xlsx'
file_name.startswith('2020')

 

* .endswith()

file_name = '보고서.xlsx'
file_name.endswith('xlsx')

 


▶ 슬라이싱

: 변수명[start(시작데이터) : end(끝 데이터) : step(간격)]

 

* 슬라이싱_설명 잘 해놓은 게시글 링크 첨부합니다 :D

https://codetorial.net/tips_and_examples/list_slicing.html
728x90