s='python\n\n'
# 직접 enter로 다음 줄로 넘겨도 됨
s
'python\n\n'
print(s)
python


  • ''' => 긴 str배정시에 사용
s='python'
s.capitalize()
'Python'
s.find('t')
2
s.count('y')
1
s.count('x')
0
s.count('p')
# 대소문자 구분X
1
s.find('a')
-1
s.index('t')
2
a='pyathaon'
a.find('a')
2
a.index('a')
2
a.isalpha()
True
s='asd342'
s.isalnum()
True
a='python 3'
a.isalnum()
False
s='123'
# 십진법!
s.isdecimal()
True
s='223311.3'
s.isdecimal()
False
s.isdigit()
False
s.isnumeric()
False
s='123123123'
s.isdecimal()
True
a='2034'
a.isdigit()
True
a='3\u00B3' 
a
'3³'
# 십진법
a.isdecimal()
False
a.isdigit()
True
a.isnumeric()
True
a='\u00BC'
a
'¼'
a.isdecimal()
False
a.isdigit()
False
a.isnumeric()
True
a='Python'
a.islower()
False
a.isupper()
False
  • 중요
a='PythonP'
a.replace('P','Q')
'QythonQ'
  • 중요
a='I like python'
# space 기준으로 나눈 뒤 리스트化
a.split(' ')
['I', 'like', 'python']
a='I,like,python'
a.split(',')
['I', 'like', 'python']

str에서 다음줄로 내릴 때

a='I like python\nHe likes python\nfucking python'
a
'I like python\nHe likes python\nfucking python'
print(a)
I like python
He likes python
fucking python
a.splitlines()
['I like python', 'He likes python', 'fucking python']
b=a.splitlines()
b[0]
'I like python'
# 맨 앞만 대문자로 변경하는 건 Caitalize
b[0].upper()
'I LIKE PYTHON'
b[0].count('L')
0

s='python'
a=3
print(s,a)
python 3
print(s,str(a))
python 3
print(s+str(a))
python3
print(s + ' ' + str(a))
python 3

Format*

'Python {}'.format(25)
'Python 25'
'Python {1} + {0}'.format(3,'asdas')
'Python asdas + 3'
'Python {} {}'.format(3,'asd')
'Python 3 asd'
s='Python {1} {0}'
s.format(3,'asd')
'Python asd 3'
s= 'My name is {} I am {} years old' 
s.format('asd',12)
'My name is asd I am 12 years old'
ver=3.8
print('python', ver)
python 3.8
print('python' + str(ver))
python3.8
print('python{}'.format(ver))
python3.8
s='python{}'.format(ver)
print(s)
python3.8

data=4321.123456
print('DATA'+str(data))
DATA4321.123456
print('DATA', data)
DATA 4321.123456
print('DATA '+str(data))
DATA 4321.123456
print('DATA= '+str(data))
DATA= 4321.123456
print('DATA={}'.format(data))
DATA=4321.123456

참고

  • {0:<10} => {0} 위치의 값을 ":<10" 10자리로 표현할건데 왼쪽 정렬
  • {1:>5} => {1} 위치의 값을 ":>5" 5자리로 표현할건데 오른쪽 정렬
  • {0:^10} => {0} 위치의 값을 ":^10" 10 자리로 표현할 건데 가운데 정렬

print('DATA= {:.2f}'.format(data))
DATA= 4321.12
print('DATA= {:.0f}'.format(data))
DATA= 4321
print('DATA={:4.0f}'.format(data))
DATA=4321
print('DATA={:5.0f}'.format(data))
DATA= 4321
print('DATA={:6.0f}'.format(data))
DATA=  4321
print('DATA={:7.0f}'.format(data))
DATA=   4321
print('DATA={:,}'.format(data))
DATA=4,321.123456
data2=454654456.1213546
print('DATA={:,}'.format(data2))
DATA=454,654,456.1213546
 
  • data자체가 float이라서 d입력하면 오류가 발생
  • int는 소수점 없으니까 .4d 이렇게 입력하면 오류 그냥 4d로 입력해야 함
data
4321.123456
print('DATA={:4d}'.format(int(data)))
DATA=4321
print('DATA={:5d}'.format(int(data)))
DATA= 4321
print('DATA={:8d}'.format(int(data)))
DATA=    4321

  • scientific notation : e라는 것을 사용하여 수를 표현해줌
print('data = {:e}'.format(data))
data = 4.321123e+03

  • % 이용해보자
print('DATA=%(x)f' % {'x' : data})
DATA=4321.123456
print('DATA=%(x).0f' % {'x' : data})
DATA=4321
print('DATA=%(x).2f' % {'x' : data})
DATA=4321.12
print('DATA=%(x)7.0f' % {'x' : data})
DATA=   4321
print('DATA=%(x)8.3f' % {'x' : data})
DATA=4321.123
print('DATA=%(x)8.0f' % {'x' : data})
DATA=    4321

import math
print(math.pi)
import numpy as np
print(np.pi)
3.141592653589793
3.141592653589793
math.inf
inf
np.inf
inf
math.nan #숫자가 아님을 표현할 때
nan
np.nan
nan
math.e
2.718281828459045
# 올림, 반올림과 다름
math.floor(math.pi)
3
# 버림, 반올림과다름
math.ceil(math.pi)
4
round(math.pi,0)
3.0
math.trunc(math.pi)
3
int(math.pi)
3
math.sqrt(math.pi)
1.7724538509055159
np.sqrt(math.pi)
1.7724538509055159
math.log(10)
2.302585092994046
math.log10(10)
1.0
math.log(math.e)
1.0
p=math.pi
math.sin(p/2)
1.0
math.sin(2*p)
-2.4492935982947064e-16
x_degree=90
x_radian=x_degree*(math.pi/180)
x_radian #pi/2랑 동일
1.5707963267948966
math.radians(90)
1.5707963267948966
math.pow(2,3)
8.0
pow(2,3)
8
2**3
8
x=2
math.isinf(x)
False
math.isnan(x)
False

*중요

print(math.fsum([1,2,3]))
print(int(math.fsum([1,2,3])))
6.0
6
# error 발생
# 원소가 int일때만 가능

file - input & output

import os
os.chdir('c:\\test')
os.mkdir('temp4')
os.chdir('temp4')
os.getcwd()
'c:\\test\\temp4'
os.chdir('..')
os.getcwd()
'c:\\test'
os.chdir('temp')
os.getcwd()
'c:\\test\\temp'
os.chdir('.')
os.getcwd()
'c:\\test\\temp'
os.mkdir('dir0')
os.mkdir('dir3')
os.mkdir('dir4')
os.getcwd()
'c:\\test\\temp'
os.listdir()
['dir0',
 'dir1',
 'dir2',
 'dir3',
 'dir4',
 'file1.txt',
 'file2.txt',
 'file3.txt',
 'file4.txt']
os.rename('dir0','dir100')
os.listdir()
['dir1',
 'dir100',
 'dir2',
 'dir3',
 'dir4',
 'file1.txt',
 'file2.txt',
 'file3.txt',
 'file4.txt']
os.rmdir('dir100')
os.listdir()
['dir1',
 'dir2',
 'dir3',
 'dir4',
 'file1.txt',
 'file2.txt',
 'file3.txt',
 'file4.txt']
import sys
# 파이썬에서 사용가능한 모듈들을 불러준 것
# sys.path
os.chdir('C:\\Users\\ehfus\\Downloads\\python Introduction')
os.chdir('c:\\test\\temp')
os.getcwd()
'c:\\test\\temp'

f=open('file1.txt','w')#파일 작성
f.closed
False
f.write('fucking')
7
f.close()
f.closed
True
with open('file2.txt','w') as f:
    f.write('sibal\n')
    f.write('jotgatne\n')
    f.write('just joke\n')
f.closed
True
fid=open('file2.txt','r')
fid.read()
'sibal\njotgatne\njust joke\n'
fid.close()
fid.closed
True
fid=open('file2.txt','r')
fid.readline()
'sibal\n'
fid.readline()
'jotgatne\n'
fid.readline()
'just joke\n'
fid.readline()
''

한 줄씩 불러옴

fid.close()
fid=open('file2.txt','r')
data=[]
data.append(fid.readline())
data.append(fid.readline())
data.append(fid.readline())
data
['sibal\n', 'jotgatne\n', 'just joke\n']

자동화

data=['string1','string2','string3']
import json
f=open('file3.txt','w')
json.dump(data,f)
f.close()
f=open('file3.txt','r')
x=json.load(f)
x
['string1', 'string2', 'string3']
f.close()
import pickle
f=open('file4.txt','wb') # 바이너리로 쓰겠다

용량을 작게 할 수 있다는 장점이 있음

pickle.dump(data,f)
f.close()
f=open('file4.txt','rb') # 바이너리 불러옴
x= pickle.load(f)
x
['string1', 'string2', 'string3']
f.close()