データ構造でーたこうぞう:文字列もじれつ
4403 ワード
文字列(String)
-endオプション値出力
使用-%出力
-使用する出力
-f文字列フォーマット
Ex. end option
```
print("Hello Python", end='')
print("Hello Python", end='\n') # default : end='\n'
```
出力値```
Hello PythonHello Python
```
Ex. using %```
# %s(str) / %f(float) / %d(integer) / %c(character)
# -------------------
print("Hello! %s" %('James'))
print("%.3f" %(3.1415))
print("%d" %(12.5))
print("%c" %('a'))
```
出力値```
Hello! James
3.142
12
a
```
Ex. using {}```
print("Welcome to {}".format('파이썬'))
print("Welcome to {}".format('3.14'))
print("Welcome to {}".format('12'))
print("Welcome to {}".format('w'))
print("Welcome to {} {} {}".format('파이썬', 3.14, '아'))
print("my favorite movie is {:03d}".format(7))
print("원주율은 ? {:.2f}".format(3.141592))
```
出力値```
Welcome to 파이썬
Welcome to 3.14
Welcome to 12
Welcome to w
Welcome to 파이썬 3.14 아
my favorite movie is 007
원주율은 ? 3.14
```
Ex. f formating```
name = John
age = 15
PI = 3.141592
print(f"My name is {name}, and I'm {age}years old.")
print(f"PI = {PI:.2f}"
```
出力値```
My name is John, and I'm 15years old.
PI = 3.14
```
- index order starts at '0'
- index reverse order starts at '-1'
Ex.
```
my_str = "python"
# -------------------
# indexing
# my_str[0] = p
# my_str[4] = o
# my_str[-1] = n
# -------------------
# slicing
# my_str[1:4] = yth
# my_str[:] = python
# my_str[2:] = thon
# my_str[:4] = pyth
# my_str[-2:] = on
# my_str[-4:-1] = tho
# my_str[-4:1] = ' ' space
# my_str[0:6:2] = pto ; step = 2
# my_str[::2] = pto ; :: means all, and step = 2
# my_str[::-1] = nohtyp ; step = -1 means reverse step 1
```
- split() : use it when you want to split the str
- join() : use it when you want to join the list
Ex.
```
movies = "미나리, 경계선, 기생충, 학교"
# ------------------------------------------------
movieList = movies.split(",")
# movieList = ['미나리', '경계선', '기생충', '학교']
# ------------------------------------------------
movie = ''.join(movieList)
# movie = "미나리 경계선 기생충 학교"
```
- lower() : makes all characters in the string lowercase
- upper() : makes all characters in the string uppercase
Ex.
```
a = "This is an apple"
#
print(a.lower()) # this is an apple
print(a.upper()) # THIS IS AN APPLE
```
- startswith("==") : return T/F : if str starts with "=="
- endswith("==") : return T/F : if str ends with "=="
Ex.
```
a = "01_star.png"
#
print(a.startswith("01")) # True
print(a.endswith("png")) # True
```
- replace("==", "--") : replace "=="(in the string) with "--"
Ex.
```
a = "01_sample.png"
a = a.replace("png", "mp3")
# a = "01_sample.mp3"
```
- lstrip() : remove left-side spaces in the string
- rstrip() : remove right-side spaces in the string
- strip() : remove both-side spaces in the string
Ex.
```
a = " 01_sample.png"
b = "01_sample.png "
c = " 01_sample.png "
#
a = a.lstrip() # a = "01_sample.png"
b = b.rstrip() # b = "01_sample.png"
c = c.strip() # c = "01_sample.png"
```
Reference
この問題について(データ構造でーたこうぞう:文字列もじれつ), 我々は、より多くの情報をここで見つけました https://velog.io/@choijungp/자료구조-문자열テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol