티스토리 뷰

Python

[Python] String Method

seungwonlee 2022. 8. 25. 16:34

String Method에 대해 알아보도록 하겠습니다.

Method는 함수라고도 하며 어떤 기능의 묶음 뜻합니다. 

 

다양한 메서드 종류와 간단하게 예제를 통해 알아보도록 하겠습니다.

 

아래 작성한 예제를 통해 확인할 수 있습니다.

a = 'Hello world'
# 알파벳 'o' 문자가 몇개 인가?
print(a.count('o'))
# 결과: 2

c = 'Hi Python'
# 'Hi'로 시작하는지?
print(c.startswith('Hi'))
# 결과: True

# 'Hi'로 끝나는지?
print(c.endswith('Hi'))
# 결과: False
print(c.endswith('Python'))
# 결과: True
# 문자열 일부 변경
print(c.replace('Python', 'JAVA'))
# 결과: Hi JAVA

# 특정위치 찾기
print(c.find('P'))
# 결과: 3 (index 기준)

d = '.Hi Python.'
# 앞뒤 특정문자 제거
print(d.strip('.'))
# 결과: Hi Python

String Method에 대해 알아보았습니다.

 

끝.

728x90

'Python' 카테고리의 다른 글

[Python] for  (0) 2022.09.05
[Python] if  (0) 2022.09.02
[Python] upper and lowercase letters  (0) 2022.08.17
[Python] index  (0) 2022.08.12
[Python] bool()  (0) 2022.08.10
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함