ABOUT ME

Today
Yesterday
Total
  • '23.3.2(목) 파이썬 입문 <2 : Print 사용법>
    코딩/파이썬 2023. 3. 2. 14:05

    <py 파일 ipynb 파일로 변환하는 방법>

    ! pip install ipynb-py-convert

    #py --> ipynb
    ! ipynb-py-convert a.py a.ipynb
    #ipynb --> py
    ! ipynb-py-convert a.ipynb a.py

    <print 다양한 옵션들>
    1. sep #separator 사용 
     - print('Hello', 'World', '!', sep=' , ')
      >> Hello,World,!
    2. end 
     - print('Hello', end =' ')
     - print('World' , end=' ')
     - print('!')
      >> Hello World !
    3. file
     - print('Hello World!', file = 'sys.stdout')
    4. format 
     - print('{} {}'.format('one','two'))
      >> one two

    - print('{1} {0}'.format('one', 'two')
     >> two one

    - print('%s %s' %('one', 'two'))
     >> one two

    - print('%10s' %('nice'))
    - print('{:>10}'.format('nice'))
     >>      nice

    -print('%-10s' %('nice'))
    -print('{:#<10}.format('nece'))
    >>nice######

    -print('%.5s' %('pythonstudy'))
    -print('{:.5}'.format('pythonstudy'))
     >>python

    - print('{:10.5}'.format('pythonstudy')) #공간 10 절삭 5
     >>python     

    -print('%d %d' %(3 , 7))
    -print('{} {}'.format(3, 7))
     >> 3, 7

    -print('%4d' %(42))
    -print('{:4d}'.format(42))
     >>42  

    -print('%f' %(3.1425363467347))
    -print('{:f}'.format(3.1425363467347))
     >>3.142536

    -print('%1.2f' %(3.1425363467347))
    -print('{:1.2f}'.format(3.1425363467347))
     >>3.14

    -print('%06.2f' %(3.1425363467347))
    -print('{:06.2f}'.format(3.1425363467347)) #06자리와 소수점 2자리까지만 나옴 
     >> 003.14

    5. 3가지 Format practices <추가 수업>
    '''참고'''
    \n  : 개행
    \t   : 행
    \\   : 문자
    \''  : 문자
    \'   : 문자
    \000   : 널 문자

    n = 'CHOI'
    x = 30
    y = 40
    text = 33523523

    ex1 = ' n = %s, s = %s, sum = %d' % (n, text, (x+y))
    ex2 = ' n = {n}, s = {s}, sum={sum}'.format( n=n, s = text, sum=x+y)
    ex3 = f'n={n}, s={text}, sum{x+y}'
     >> n = CHOI, s = 33523523, sum = 70


    6. 구분기호
    m = 100000000
    print(f'm : {m:,}')
    >>100,000,000

    7. 정렬
    ^ : 가운데
    < : 왼쪽 
    > : 오른쪽

    8. f 스트링
    t = 20
    print(f't : {t::>10}')
    >>        20

    print(f't : {t::<10}')
    >>20       

    print(f't : {t::#^10}') 
     >>####10####


    댓글

Designed by Tistory.