개발/Python

[기본 2]Python의 문자열함수, print, input

cozynow 2017. 12. 28. 00:12
str

Python의 문자열 다루기

In [4]:
import pandas as pd
#url = "http://cozy.ddns.net:3888/notebooks/PythonSchool/aTO0.csv"
#pd.read_csv(url, nrows=2)

raw_data ={
            'Data':['A','1','2','3','4','5','6','7','8','9','0']
          }
df = pd.DataFrame(raw_data )
df
Out[4]:
Data
0 A
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 0
In [2]:
aTO0 = 'A1234567890'
#0 부터 시작하니 2인경우 2이 나온다.
print("aTO0[2] = {}".format(aTO0[2]))   

#0부터 2-1 까지 
print("aTO0[0:2] = {}".format(aTO0[0:2])) 

#0부터 5-1까지
print("aTO0[:5] = {}".format(aTO0[:5])) 

 #젤 뒤가 -1 이므로 -2이면 뒤에서 앞으로 한자리 앞이 나옴
print("aTO0[-2] = {}".format(aTO0[-2]))

#2자리부터 끝까지 2칸씩 출력
print("aTO0[1::2] = {}".format(aTO0[1::2]))

#0부터 5-1까지 2칸씩
print("aTO0[:5:2] = {}".format(aTO0[:5:2]))

#9에서 3+1까지 -1칸씩
print("aTO0[9:3:-2] = {}".format(aTO0[9:3:-2]))

a = "hello"
b = 'world'

print(a +' '+ b)

x = '1 + 3 ='
y = 4

#숫자와 문자를 연산할 수 없다.
#print(x + y)

#이건 되어야 함????
print(x + str(y))

print("문자길이 len(a+b) = {}".format(len(a+b)))
aTO0[2] = 2
aTO0[0:2] = A1
aTO0[:5] = A1234
aTO0[-2] = 9
aTO0[1::2] = 13579
aTO0[:5:2] = A24
aTO0[9:3:-2] = 975
hello world
1 + 3 =4
문자길이 len(a+b) = 10

%를 이용한 서식 출력

print(' '%())

  • 문자열 : %s
  • 정수 : %d
  • 실수 : %f
In [22]:
name = 'akami'.title()
score = 99.999
#%10s는  10자에 문자열을 넣는다.
print("^%10s got ^%2.2f score."%(name, score))
^     Akami got ^100.00 score.

input 함수

In [28]:
x = int(input("Enter your number : "))
print(x * 3)
Enter your number : 4
12