본문 바로가기
Python

Python 함수정리 (문자열)

by 글로리. 2020. 5. 8.

print( ) : 괄호안에 있는 내용을 나타냄

1
2
>>>print('hello python')
hello python
 
 

 

abs( ) : 괄호안 숫자를 절대값형태로 나타냄
1
2
3
4
5
6
7
>>> a = 3.14
>>> print(abs(a))
3.14
 
>>> b = -3.14
>>> print(abs(b))
3.14 
 

 

문자열.format( ) : 문자열의 대괄호 자리에 format 뒤의 괄호안에 들어있는 값을 하나씩 넣는다

1
2
3
4
5
>>> str = '이 연필은{0} {1}입니다.'
 
>>> print(str.format(1200,원))
 
이 연필은 1200 원 입니다.
 

 

len( ) : 파라미터로 전달된 문자열의 글자수 카운트

 - 띄어쓰기도 카운트에 포함.

1
2
3
4
5
>>> str = 'Life is short, You need Python'
 
>>> print(len(str))
 
30
 

 

 

count( ) : 특정 글자나 단어의 개수 세기

1
2
3
4
5
>>> str = 'Life is short, You need Python'
 
>>> print(str.count('i'))
 
2
 

 

find( ) : 특정 글자나 단어가 처음 등장하는 위치 조회

 - 찾지 못했을 경우 -1 리턴

1

2

3

4

5
>>> str = 'Life is short, You need Python'
 
>>> print(str.find('i'))
 
1

>>> print(str.find('z'))

-1
 

 

 

 

rfind( ) : 특정 글자나 단어가 마지막으로 등장하는 위치 조회

1
2
3
4
5
>>> str = 'Life is short, You need Python'
 
>>> print(str.rfind('i'))
 
5
 

 

index( ) : 특정 글자나 단어가 처음 등장하는 위치 조회

 - 찾지 못했을 경우 에러 발생

1
2
3
4
5
>>> str = 'Life is short, You need Python'
 
>>> print(str.index('short'))
 
8
cs

 

 

startswith( ) : 특정 단어나 글자로 끝나는지 여부 검사

                  (True, False로 반환)

 

1
2
3
4
5
6
7
8
9
>>> str = 'Life is short, You need Python'
 
>>> print(str.startswith('L'))
 
True
 
>>> print(str.startswith('A'))
 
False
cs

 

endswith( ) : 특정 단어나 글자로 끝나는지 여부 검사

                 (True, False로 반환)

 

1
2
3
4
5
6
7
8
9
>>> str = 'Life is short, You need Python'
 
>>> print(str.endswith('n'))
 
True
 
>>> print(str.endswith('A'))
 
False
cs

 

upper( ) : 모든 글자를 대문자로 변환

1
2
3
4
5
6
>>> str = 'Life is short, You need Python'
 
>>> print(str.upper())
 
LIFE IS SHORT, YOU NEED PYTHON
 
cs

 

lower( ) : 모든글자를 소문자로 변환

1
2
3
4
5
6
>>> str = 'Life is short, You need Python'
 
>>> print(str.lower())
 
life is short, you need python
 
cs

 

swapcase( ) : 대문자 -> 소문자, 소문자 -> 대문자 변환

1
2
3
4
5
6
>>> str = 'Life is short, You need Python'
 
>>> print(str.swapcase())
 
lIFE IS SHORT, yOU NEED pYTHON
 
cs

 

capitalize( ) : 문장의 첫 글자를 대문자로 변환

1
2
3
4
5
6
>>> str = 'Life is short, You need Python'
 
>>> print(str.capitalize())
 
Life is short, you need python
 
cs

 

title( ) : 각 단어의 첫 글자를 대문자로 변환

1
2
3
4
5
>>> str = 'Life is short, You need Python'
 
>>> print(str.title())
 
Life Is Short, You Need Python
cs

 

replace( ) : 변수.replace(A,B), 변수에서 A를 B로 변경

1
2
3
4
5
>>> str = 'Life is short, You need Python'
 
>>> print(str.replace('Life','Your height'))
 
Your height is short, You need Python
cs

 

lstrip( ) : 왼쪽 공백을 지운 결과 반환

1
2
3
4
5
>>> k = '    pyhton    '
 
>>> print(k.lstrip())
 
python_____
cs

 

rstrip( ) : 오른쪽 공백을 지운 결과 반환

1
2
3
4
5
>>> k = '    pyhton    '
 
>>> print(k.rstrip())
 
____python
cs

 

strip( ) : 좌우 공백을 지운 결과 반환

1
2
3
4
5
>>> k = '    pyhton    '
 
>>> print(k.strip())
 
python
cs

 

join( ) : 파라미터로 전달되는 문자열의 각 글자사이에 변수값을 삽입

1
2
3
4
5
6
7
>>> a = ','
 
>>> b = a.join('pyhton')
 
>>> print(b)
 
p,y,t,h,o,n
cs

'Python' 카테고리의 다른 글

Python(딕셔너리)  (0) 2020.05.13
리스트  (0) 2020.05.12
문자열, 튜플  (0) 2020.05.12
변수, 함수, 객체의 이해  (0) 2020.05.11
Python 시작  (0) 2020.05.11

댓글