# 4.9.4.     문자열 포맷 연산자

문자열 포맷 연산자 % 는 C의 printf() 함수와 비슷합니다. 일정한 포맷에 맞춰 문자열을 조합하는 것을 문자열 포맷팅이라 합니다.

```
print "My name is %s and weight is %d kg!" % ('Zara', 21)
```

위의 코드가 실행 되면 다음과 같은 결과를 출력 합니다.

My name is Zara and weight is 21 kg!

다음은 % 와 함께 사용할 수 있는 변환 지시어 입니다.

| Format Symbol | Conversion                                      |
| ------------- | ----------------------------------------------- |
| %c            | character                                       |
| %s            | string conversion via str() prior to formatting |
| %i            | signed decimal integer                          |
| %d            | signed decimal integer                          |
| %u            | unsigned decimal integer                        |
| %o            | octal integer                                   |
| %x            | hexadecimal integer (lowercase letters)         |
| %X            | hexadecimal integer (UPPERcase letters)         |
| %e            | exponential notation (with lowercase 'e')       |
| %E            | exponential notation (with UPPERcase 'E')       |
| %f            | floating point real number                      |
| %g            | the shorter of %f and %e                        |
| %G            | the shorter of %f and %E                        |

기타 지원되는 기호 및 기능은 다음 표에 나열되어 있습니다.

| Symbol | Functionality                                                                                                      |
| ------ | ------------------------------------------------------------------------------------------------------------------ |
| \*     | argument specifies width or precision                                                                              |
| -      | left justification                                                                                                 |
| +      | display the sign                                                                                                   |
| \<sp>  | leave a blank space before a positive number                                                                       |
| #      | add the octal leading zero ( '0' ) or hexadecimal leading '0x' or '0X', depending on whether 'x' or 'X' were used. |
| 0      | pad from left with zeros (instead of spaces)                                                                       |
| %      | '%%' leaves you with a single literal '%'                                                                          |
| (var)  | mapping variable (dictionary arguments)                                                                            |
| m.n.   | m is the minimum total width and n is the number of digits to display after the decimal point (if appl.)           |

format 함수는 문자를 다양한 형태로 포맷팅하는데 사용합니다. 위치를 기준으로 하는 인덱스형, 필드명을 기준으로 하는 키워드형, 리스트 인덱스형으로 사용할 수 있습니다.

```python
 sub1 = "python string!"
 sub2 = "an arg"

 a = "i am a %s" % sub1
 b = "i am a {0}".format(sub1)
 c = "with %(kwarg)s!" % {'kwarg':sub2}
 d = "with {kwarg}!".format(kwarg=sub2)

 print(a)    # "i am a python string!"
 print(b)    # "i am a python string!"
 print(c)    # "with an arg!"
 print(d)    # "with an arg!"

 # 위치를 기준으로 한 포맷팅
 print('We are the {} who say "{}!"'.format('student', 'children'))
 print('We are the {0} who say "{1}!"'.format('student', 'children'))
 # 필드명을 기준으로 한 포맷팅
 print("Age of student {name} is {age}".format(age=29, name='James'))
 # object의 인덱스 혹은 키를 사용하여 포맷팅
 pos = [12.5, 35, 90]
 print("A의 좌표는 x = {p[0]}, y = {p[1]}, z = {p[2]}".format(p=pos))
```


---

# 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://sdc-james.gitbook.io/onebook/3./3.9.-strings/3.9.4..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.
