# 문자열과 문자열 연산 (string)

{% hint style="info" %}
**문자열**
{% endhint %}

```python
# <class 'str'>
string = "Hi"
print(type(string))

print("Hello world")

print('백문이불여일견 백견이불여일타')

print("123")

# 문자열 안에서 따옴표, 쌍따옴표 사용 
print("문자열은 문자, 단어들의 '집합'입니다.")
# 문자열은 문자, 단어들의 '집합'입니다.

print('문자열은 문자, 단어들의 "집합"입니다.')
# 문자열은 문자, 단어들의 "집합"입니다.

# 이스케이프 문자 \\
print("\\'이스케이프\\"")
# '이스케이프"

print("\\'이스케이프\\"\\\\")
# '이스케이프"\\

print("백문이불여일견\\n백견이불여일타")
# 줄바꿈
# 백문이불여일견
# 백견이불여일타

print(r"백문이불여일견\\n백견이불여일타")
# 이스케이프 문자 무시
# "백문이불여일견\\n백견이불여일타"
```

{% hint style="info" %}
**문자열 연산**
{% endhint %}

파이썬의 문자열은 아주 다양하고 강력한 기능들을 제공합니다. :thumbsup:

```python
hi = "안녕"

more_politely = "하세요"

print(hi + more_politely) # 안녕하세요

print(hi*10) # 안녕안녕안녕안녕안녕안녕안녕안녕안녕안녕

# 문자열의 길이
print(len(hi)) # 2

# 문자열의 인덱싱과 슬라이싱
print(hi[0]) # 안
print(hi[-1]) # 녕
print(more_politely[:2]) # 하세
print(more_politely[2]) # 요

# 문자열 바꾸기
# 문자열.replace(타겟문자, 바꿀문자)
print(more_politely.replace("하세요", "요세하")) # 요세하

# 문자열 포멧팅
age = 10

print("%s%s, 저는 누구누구입니다" %(hi, more_politely))

print("%s%s, 저는 누구누구입니다. %d살이에요" %(hi, more_politely, age))

# format 함수를 이용한 포멧팅
print("{0}{1}, 저는 누구누구입니다".format(hi, more_politely))

# f-string
print(f"{hi}{more_politely}, 저는 누구누구입니다.")

# 소수점 표현 f-string
pi = 3.1415926535

# f"{실수:몇번째자리까지}" 
print(f"{pi:0.2f}") # 소수점 3번째 자리에서 반올림됩니다. 3.14
print(f"{pi:0.3f}") # 소수점 4번째 자리에서 반올림됩니다. 3.142

# 문자열 관련 함수들
string = "aabbaeda"

# 개수 세기
print(string.count("a")) # 4

# 위치 찾기
print(string.find("e")) # 5
print(string.find("b")) # 찾는 문자가 여러개라면 가장 첫번째자리를 반환합니다. 2
print(string.find("z")) # -1 존재하지 않으면 -1을 반환합니다.

# index라는 함수도 위치를 찾는데 사용되나
# 차이로는 존재하지 않을시 에러를 발생시킵니다.
print(string.index("z")) # ValueError: substring not found

# 문자열 삽입 
print(".".join(string)) # 각각 문자 사이에 "."을 삽입 a.a.b.b.a.e.d.a

# 대문자 소문자 변환
print(string.upper()) # AABBAEDA
print("AABBAEDA".lower()) # aabbaeda

# 공백 지우기
blank_string = "    blank string   "

# 오른쪽 공백 지우기
print(blank_string.rstrip()) # "   blank string"
# 왼쪽 공백 지우기
print(blank_string.lstrip()) # "blank string   "
# 양쪽 공백 지우기
print(blank_string.strip()) # "blank string"

# 문자열 나누기
split_string = "this : split string"

# split(기준으로나눌문자)
# split() 공백을 기준으로 나누겠다.
# split(":") : 을 기준으로 나누겠다
print(split_string.split()) # ['this', ':', 'split', 'string']
print(split_string.split(":")) # ['this ', ' split string']
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nmdkims-organization.gitbook.io/python-hand-book/day1./1.-numbers-strings-lists-dictionaries/string.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
