본문 바로가기
코딩/Python 기초

Python 문자열 자료형

by 미생22 2023. 1. 5.
728x90

1. python 설치

1) python 설치

www.python.org/downloads

2) 아나콘다 설치

http://www.anaconda.org

 

2. 문자열 자료형

1) indexing

>>>a = ‘bacdef’

>>>a[0]

‘b’

 

2) slicing

a[시작번호:끝번호] : 시작번호부터 끝번호 전까지

 

3) formating

>>>number = 3

>>> “I eat %d %s” %(number,“apples”)

‘I eat 3 apples

 

python3.6부터 f문자열 포매팅 기능 제공

>>>name = ‘홍설

>>>age = 30

>>>f’내이름은 {name}이고 나이는 {age}입니다.‘

 

4) upper

>>>a=’hi’

>>>a.upper()

‘HI’

 

5) 공백채우기

>>> "{0:=^10}".format("hi")

'====hi===='

>>> "{0:!<10}".format("hi")

'hi!!!!!!!!'

 

6) count

>>> a = "hobby"

>>> a.count('b')

2

 

7) 위치 알려주기1(find)

>>> a = "Python is the best choice"

>>> a.find('b')

14

>>> a.find('k')

-1 #없으면 -1반환

 

8) 위치 알려주기2(index)

>>> a = "Life is too short"

>>> a.index('t')

8 #없으면 에러

 

9) strip

>>> a = " hi "

>>> a.strip()

'hi'

 

10) replace

>>> a = "Life is too short"

>>> a.replace("Life", "Your leg")

'Your leg is too short'

 

11) split

>>> a = "Life is too short"

>>> a.split()

['Life', 'is', 'too', 'short']

>>> b = "a:b:c:d"

>>> b.split(':')

['a', 'b', 'c', 'd']

 

3. 사용자 입력과 출력

1) input()

사용자가 입력한 값을 어떤 변수에 대입하고 싶을 때 사용

input은 입력되는 모든 변수를 문자열로 취급

>>> number = input("숫자를 입력하세요: ")

숫자를 입력하세요: 3

>>> print(number)

3

728x90