튜토리얼에도 고난과 역경이 있다.

Python) format() 본문

나의 공부/프로그래밍 언어

Python) format()

내가 Nega 2022. 1. 3. 10:54
728x90

문자열 포맷팅 text formatting

  • 기본적인 사용 방법

    >>> print("hi, my name is {}".format("호이얏"))
    hi, my name is 호이얏
    >>> print("{} is a horse in korean".format("말"))
    말 is a horse in korean
  • 인덱스 사용

    >>> print("책 제목 : {1}, 출판사 : {0}, 가격 : {2}".format("길벗", "이 책은 누구의 책이냐?", 9500))
    책 제목 : 이 책은 누구의 책이냐?, 출판사 : 길벗, 가격 : 9500
    >>> print("a: {3}, b: {1}, c:{0}".format(3,2,"-",1))
    a: 1, b: 2, c:3
  • 변수 지정

    >>> print("책 제목 : {a}, 출판사 : {b}, 가격 : {c}".format(b="길벗", a="이 책은 누구의 책이냐?", c=9500))
    책 제목 : 이 책은 누구의 책이냐?, 출판사 : 길벗, 가격 : 9500
    >>> print("a: {a}, b: {c}, c:{d}".format(d=3,c=2,b="-",a=1))
    a: 1, b: 2, c:3
  • 형식 변경

    >>> format(10000,",")
    '10,000'

진수 변환

  • 10진수 -> 16진수

    "{0:x}".format(10진수)

    >>> "{0:x}".format(1669)
    '685'
    >>> "{0:x}".format(9999)
    '270f'
    >>> "{0:x}".format(1111)
    '457'
    >>> "{0:x}".format(1000)
    '3e8'
  • 10진수 -> 8진수

    "{0:o}".format(10진수)

    >>> "{0:o}".format(1669)
    '3205'
    >>> "{0:o}".format(9999)
    '23417'
    >>> "{0:o}".format(1111)
    '2127'
    >>> "{0:o}".format(1000)
    '1750'
  • 10진수 -> 2진수

    "{0:b}".format(10진수)

    >>> "{0:b}".format(1669)
    '11010000101'
    >>> "{0:b}".format(9999)
    '10011100001111'
    >>> "{0:b}".format(1111)
    '10001010111'
    >>> "{0:b}".format(1000)
    '1111101000'
반응형