Notice
Recent Posts
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- viewmodel
- android
- intent
- 안드로이드
- scroll
- activity
- Compose
- 앱
- appbar
- textview
- CustomScrollView
- binding
- Flutter
- Kotlin
- 앱바
- drift
- livedata
- textfield
- DART
- ScrollView
- data
- Navigation
- 테스트
- LifeCycle
- Button
- tabbar
- TEST
- Dialog
- 계측
- Coroutines
Archives
- Today
- Total
Study Record
[파이썬] 문자열 인덱싱/슬라이싱, 포맷팅, 관련 함수 본문
728x90
※ 순서형 자료형은 문자열 인덱싱/슬라이싱을 제공한다. ( str, list, tuple, range )
문자열 인덱싱
→ w = "HELLO" 일 경우,
문자열 슬라이싱
word = "Hello World!"
print(word[0:3]) # "Hel"
print(word[3:7]) # "lo W"
print(word[0:-2]) # "Hello Worl"
print(word[4:]) # "o World!"
# word[0:3] : 0번째 문자부터 3번째 문자 전까지!
문자열 곱하기
a = "Hello"
b = "Wow!"
print(b*3) # "Wow!Wow!Wow!"
print((a+b) * 2) # "HelloWow!HelloWow!"
문자열 포맷팅
a = "banana"
print("I eat %s apples." % "five") # "I eat five apples."
print("%c eat apples." % 'I') # "I eat apples."
print("I eat %d apples." % 3) # "I eat 3 apples."
print("I eat %f apples." % 3.5) # "I eat 3.500000 apples."
print("Octal : %o" % 17) # "Octal : 21"
print("Hexdecimal : %x" % 0x65) # "Hexdecimal : 65"
print("Literal : %%") # "Literal : %%"
print("I eat %d %s" % (4, a)) # "I eat 4 banana"
문자열 관련 함수 - len(), count(), find(), lower(), upper(), split(), list(), join(), replace(), strip(), lstrip(), rstrip()
a = "I love apples, apple are my favorite fruit"
print(len(a)) # a 의 길이 : 42
print(a.count('a')) # a 에서 'b' 의 개수 : 4
print(a.count('a', 14, 20)) # a[14:21] 에서 'b'의 개수 : 1
print(a.find('o')) # a 에서 'o' 가 첫번째로 검색된 index : 3
print(a.find('o', 10, len(a) - 1)) # a[10:] 에서 'o' 가 첫번째로 검색된 index : 31
"""
a.index() 와 a.find() 는 같은 함수지만
index()는 존재하지 않으면 오류가 나고 find()함수는 -1을 리턴한다.
"""
sep = ','
print(sep.join('abdc')) # a,b,c,d
print("".join(['a', 'b', 'c', 'd'])) # abcd
print("".join({'a', 'b', 'c', 'd'})) # abcd
"""
join() 은 문자열로 만들어준다. 주로 list -> string
"""
b = "i love you"
c = "I EAT HAMBUGER"
print(b.upper()) # "I LOVE YOU"
print(c.lower()) # "i eat hambuger"
c.startswith('I') # c 가 I 로 시작하는지 true/ false
c.endswith('R') # c 가 R 로 끝나는지 true / false
c.lower().startwith('i')
d = " iiii "
print(d.lstrip()) # 왼쪽 공백 제거 : "iiii "
print(d.rstrip()) # 오른쪽 공백 제거 : " iiii"
print(d.strip()) # 양쪽 공백 제거 : "iiii"
e = "Life is too short"
f = "a;b;c;d;e"
print(e.replace("Life", "Your leg")) # 문자열 치환 : "Your leg is too short"
print(e.split()) # 문자열 쪼개기 : ['Life', 'is', 'too', 'short']
print(f.split(';')) # 문자열 쪼개기 : ['a', 'b', 'c', 'd', 'e']
print(e.list()) # ['L', 'i', 'f', 'e', ' ', 'i', 's', ' ', 't', 'o', 'o', ' ', 's', 'h', 'o', 'r', 't']
"""
split() 는 주로 string -> list
"""
728x90
'서버보안 > 리눅스 서버보안' 카테고리의 다른 글
[파이썬] 딕셔너리(JSON) (0) | 2021.11.24 |
---|---|
[파이썬] 튜플(tuple) (0) | 2021.11.24 |
[파이썬] 리스트 (0) | 2021.11.24 |
[파이썬] 기초 (0) | 2021.11.23 |
[파이썬] Pycharm 단축키 (0) | 2021.11.23 |