๐Ÿ“—
JunegLee's TIL
  • TIL
  • python
    • class
    • String Basic
    • regularExpression
    • String function
    • Generator
    • String format
    • getset
    • module
    • while
    • numpy
    • print()
    • matplotlib
    • for
    • Boolean
    • tuple
    • package
    • input(variable)
    • list
    • if
    • file
    • type()
    • pandas
    • function
    • dictionary
    • ๊ตฌ๋ฌธ ์˜ค๋ฅ˜์™€ ์˜ˆ์™ธ
    • builtinFunction
    • Constructor
  • algorithm
    • sort
      • mergeSort
      • insertionSort
      • bubbleSort
      • heapSort
      • quickSort
      • selectionSort
    • recursion
    • Greedy
    • DepthFirstSearch
    • basic
      • DataStructure
    • hash
    • BreadthFirstSearch
  • tensorflow
    • keras
      • layers
        • Flatten
        • Flatten
        • Dense
        • Dense
        • Conv2D
        • Conv2D
    • tensorflow1x
    • tensorflow2x
  • DB
    • setting
    • join
    • subQuery
    • overview
  • deep-learning
    • neuralNetwork
    • perceptron
    • neuralNetworkLearning
    • convolution neural network
    • Gradient Descent
    • Linear Regression
    • backPropagation
    • logistic regression
    • overview
  • textPreprocessing
    • overview
  • java
    • basics
      • generic
      • Variable
      • String
    • theory
      • Object Oriented Programing
  • NLP
    • Embedding
    • Natural Language Processing
Powered by GitBook
On this page
  • format
  • ๋ฌธ์ž์—ด ํฌ๋งท ์ฝ”๋“œ
  • ํฌ๋งท ์ฝ”๋“œ ์‚ฌ์šฉ
  • format ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉ : "{}".format(x)
  • format ํ•จ์ˆ˜์˜ ์ •๋ ฌ๊ณผ ๊ณต๋ฐฑ
  • format ํ•จ์ˆ˜์™€ ์Šฌ๋ผ์ด์‹ฑ ์ด์šฉ

Was this helpful?

  1. python

String format

format

  • ๋ฌธ์ž์—ด์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํ•จ์ˆ˜

  • "{}".format(10) ํ˜•์‹์ด๋ฉฐ

  • ์ค‘๊ด„ํ˜ธ์˜ ๊ฐœ์ˆ˜์™€ ๊ด„ํ˜ธ์•ˆ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋ฐ˜๋“œ์‹œ ๊ฐ™์•„์•ผํ•œ๋‹ค

    String_a = "{}".format(10)
    String_b = "{} {}".format(10, 20)
    String_c = "{} {} {}".format(10, 20, 30)  
    print(String_a) # 10
    print(String_b) # 10 20 
    print(String_c) # 10 20 30

๋ฌธ์ž์—ด ํฌ๋งท ์ฝ”๋“œ

์ฝ”๋“œ

์„ค๋ช…

%s

๋ฌธ์ž์—ด(String)

%c

๋ฌธ์ž 1๊ฐœ(Character)

%d

์ •์ˆ˜(Integer)

%f

๋ถ€๋™์†Œ์ˆ˜(floating-point)

%o

8์ง„์ˆ˜

%x

16์ง„์ˆ˜

%%

Literal % (๋ฌธ์ž % ์ž์ฒด)

ํฌ๋งท ์ฝ”๋“œ ์‚ฌ์šฉ

>>> "I eat %s apples." % "five"
'I eat five apples.'
>>> "I eat %d apples." % 3
'I eat 3 apples.'
>>> number = 3
>>> "I eat %d apples." % number
'I eat 3 apples.'
>>> number = 10
>>> day = "three"
>>> "I ate %d apples. so I was sick for %s days." % (number, day)
'I ate 10 apples. so I was sick for three days.'
  • ์ •๋ ฌ๊ณผ ๊ณต๋ฐฑ

    >>> "%10s" % "hi"
    '        hi'
    >>> "%-10sjane." % 'hi'
    'hi        jane.'
    >>> "%0.4f" % 3.42134234
    '3.4213'
    >>> "%10.4f" % 3.42134234
    '    3.4213'

format ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉ : "{}".format(x)

  • ์ˆซ์ž ๋Œ€์ž…

    >>> "I eat {0} apples".format(3) 
    # {0}์€ ์ธ๋ฑ์Šค๋กœ format ํ•จ์ˆ˜์˜ 0๋ฒˆ์งธ ๊ฐ’์œผ๋กœ ์ž…๋ ฅํ•˜๊ณ  ์ถœ๋ ฅํ•œ๋‹ค 
    'I eat 3 apples'
  • ๋ฌธ์ž์—ด ๋Œ€์ž…

    "{0} coffee".format("five")
    'five coffee'
  • 2๊ฐœ์ƒ์˜ ๊ฐ’ ๋„ฃ๊ธฐ

    >>> "I ate {number} apples. so I was sick for {day} days.".format(number=10, day=3)
    'I ate 10 apples. so I was sick for 3 days.'
  • ์†Œ์ˆ˜์  ํ‘œํ˜„ํ•˜๊ธฐ

    >>> y = 3.42134234
    >>> "{0:0.4f}".format(y)
    '3.4213'
    >>> "{0:10.4f}".format(y)
    '    3.4213'
  • { ๋˜๋Š” } ๋ฌธ์ž ํ‘œํ˜„ํ•˜๊ธฐ

    >>> "{{ and }}".format()
    '{ and }'

format ํ•จ์ˆ˜์˜ ์ •๋ ฌ๊ณผ ๊ณต๋ฐฑ

  • ์™ผ์ชฝ ์ •๋ ฌ (:<)

    >>> "{0:<10}".format("hi")
    'hi        '
  • ์˜ค๋ฅธ์ชฝ ์ •๋ ฌ (:>)

    >>> "{0:>10}".format("hi")
    '        hi'
  • ๊ฐ€์šด๋ฐ ์ •๋ ฌ (:^)

    >>> "{0:^10}".format("hi")
    '    hi    '
  • ์ •๋ ฌ ํ›„ ๊ณต๋ฐฑ์ฑ„์šฐ๊ธฐ

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

format ํ•จ์ˆ˜์™€ ์Šฌ๋ผ์ด์‹ฑ ์ด์šฉ

  • format() ํ•จ์ˆ˜์˜ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ

    output_a = "{:d}".format(52)     #52
  • ํŠน์ • ์นธ์— ์ถœ๋ ฅ

    output_b = "{:5d}".format(52)    #   52
    output_c = "{:10d}".format(52)   #        52
  • ๋นˆ์นธ์„ 0์œผ๋กœ ์ฑ„์šฐ๊ธฐ

    output_d = "{:05d}".format(52)   #00052
    output_e = "{:05d}".format(-52)  #-0052
  • ๊ธฐํ˜ธ์™€ ํ•จ๊ป˜ ์ถœ๋ ฅํ•˜๊ธฐ

    output_f = "{:+d}".format(52)    #+52 
    output_g = "{:+d}".format(-52)   #-52 
    output_h = "{: d}".format(52)    # 52 # ๊ณต๋ฐฑ์ด ์ฑ„์›Œ์ง€์ง€ ์•Š๋Š”๋‹ค  
    output_i = "{: d}".format(-52)   #-52
  • ์กฐํ•ฉํ•˜๊ธฐ

    output_j = "{:+5d}".format(52)   #  +52 
    output_k = "{:+5d}".format(-52)  #  -52
    output_l = "{:=+5d}".format(52)  #+  52
    output_m = "{:=-5d}".format(-52) #-  52 
    output_n = "{:+05d}".format(52)  #+0052
    output_o = "{:-05d}".format(-52) #-0052
  • ๋ถ€๋™ ์†Œ์ˆ˜์  ์ถœ๋ ฅ์˜ ๋‹ค์–‘ํ•œ ํ˜•ํƒœ

    output_1 = "{:f}".format(52.273) #52.273000 # ์ •์ˆ˜์™€ ์œ ์‚ฌํ•˜๋‹ค 
    output_2 = "{:15.3f}".format(52.273) # ... 52.273
    output_3 = "{:15.2f}".format(52.273) # ...  52.27
    output_4 = "{:15.1f}".format(52.273) # ...   52.2
  • ์˜๋ฏธ ์—†๋Š” ๋ถ€๋™ ์†Œ์ˆ˜์  ์ œ๊ฑฐ

    output_g1 = 52.0
    output_g2 = "{:g}".format(output_g1)
    print(output_g1) # 52.0 
    print(output_g2) # 52
PreviousGeneratorNextgetset

Last updated 4 years ago

Was this helpful?