[Python] string

rhkr9080·2022년 10월 10일
0

Python

목록 보기
1/2

string 선언 방법

  1. 큰 따옴표(")로 양쪽 둘러싸기
"Hello World"
  1. 작은 따옴표(')로 양쪽 둘러싸기
'Hello World'
  1. 큰 따옴표 3개를 연속(""")으로 써서 양쪽 둘러싸기 (문자열 안에 `을 넣을 때)
"""Life is too short, You need python"""
  1. 작은 따옴표 3개를 연속(```)으로 써서 양쪽 둘러싸기 (문자열 안에 "을 넣을 때)
```Life is too short, You need python```

string 붙이기

>>> head = "Hello"
>>> tail = " World"
>>> head + tail
"Hello World"
>>> print("=" * 50)
==================================================

string 길이 구하기

>>> a = "Life is too short"
>>> len(a)
17

string 인덱싱 활용하기

>>> a = "Life is too short, You need Python"
>>> a[0]
'L'
>>> a[12]
's'
>>> a[-1]
'n'
>>> a[-5]
'y'
>>> a[0:4]
'Life'
>>> a[19:]
'You need Python'
>>> a[:17]
'Life is too short'
>>> a[:]
'Life is too short, You need Python'

split(seperator, maxsplit)

  • seperator : 구분할 문자
  • maxsplit : 최대 나누는 횟수
string = "Hello, World, Python"
list = string.split(' ', 2);
print(list)
>>> ['Hello', 'World', 'Python']

replace()

>>> a = "Life is too short"
>>> a.replace("Life", "Your leg")
'Your leg is too short'

string formatting

>>> name = '홍길동'
>>> age = 30
>>> f'나의 이름은 {name}입니다. 나이는 {age}입니다.'
'나의 이름은 홍길동입니다. 나이는 30입니다.'

출처
https://wikidocs.net/13

profile
공부방

0개의 댓글