[연산자]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
= 2.4 
= 2
 
print (a + b)  # 합 
print (a - b)  # 차
print (a * b)  # 곱
print (a / b)  # 나눗셈
print (a // b) # 몫
print (a % b)  # 나머지
print (a**b)   # 제곱
 
결과 값
 
4.4
0.3999999999999999
4.8
1.2
1.0
0.3999999999999999
5.76
cs


[문자열]

1
2
3
4
5
6
7
8
9
# 문자열
 
s1 = "Hello"
s2 = 'Hirit'
 
print(s1, s2)
 
# 결과값
Hello Hirit
cs


[문자열 연결]
+로 연결

1
2
3
4
5
6
7
8
9
# 문자열 연결
# + 로 연결
s3 = s1 + " " + s2 
 
print(c)
 
# 결과
Hello Hirit
 
cs


[문자열 반복]
*로 횟수 지정

1
2
3
4
5
6
7
8
9
10
11
12
13
# 문자열 반복
# *로 횟수 지정 
s4 = s3 + "\n"
 
print(s4 * 5)
 
# 결과값
Hello Hirit
Hello Hirit
Hello Hirit
Hello Hirit
Hello Hirit
 
cs


[escape code]

이스케이프 코드란 프로그래밍할 때 사용할 수 있도록 미리 정의해 둔 "문자 조합"이다.

주로 출력물을 보기 좋게 정렬하는 용도로 이용된다. 몇 가지 이스케이프 코드를 정리하면 다음과 같다.

코드설명
\n개행 (줄바꿈)
\t수평 탭
\\문자 "\"
\'단일 인용부호(')
\"이중 인용부호(")
\r캐리지 리턴
\f폼 피드
\a벨 소리
\b백 스페이스
\000널문자

이중에서 활용빈도가 높은 것은 \n, \t, \\, \', \"이다. 나머지는 프로그램에서 잘 사용되지 않는다.


[문자열 줄 바꾸기]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 문자열 줄 바꾸기
 
str1 = "Good to see\n you again"
 
str2 = '''Good to see 
you again'''
 
print(str1)
print(str2)
 
 
# 결과값
Good to see
 you again
Good to see 
you again
cs


[문자열 슬라이싱]

1
2
3
4
5
6
7
8
9
10
11
12
# 문자열 슬라이싱
 
print(s3[0]) # 0부터 시작한다
print(s3[0:3]) # 0이상 3미만 번째 까지 슬라이싱
print(s3[3:]) # 3부터 끝까지 슬라이싱 
print(s3[-1]) # 뒤에서 1 번째 
 
# 결과값
H
Hel
lo Hirit
t
cs



[문자열 포맷팅 ]

문자열 포맷 코드

코드

설명

%s

문자열 (String)

%c

문자 1(character)

%d

정수 (Integer)

%f

부동소수 (floating-point)

%o

8진수

%x

16진수

%%

Literal % (문자 자체)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 문자열 포맷팅

s1 = "Hi %s!" % "Hirit"
s2 = "Hi %d!" % 1234
s3 = "Hi %f!" % 1.24
s4 = "HI %s!, %s!" % ("Hirit","Soonduck")
print(s1) ; print(s2) ; print(s3) ; print(s4)
 
#결과값 
Hi Hirit!
Hi 1234!
Hi 1.240000!
HI Hirit!, Soonduck!
cs


포맷 코드 숫자와 함께 사용

1
2
3
4
5
6
7
8
9
10
11
12
13
# 숫자 활용 포맷 지정
 
print("%10s" % "hi"# 전체 길이가 10개인 문자열 공간에서 hi를 오른쪽으로 정렬
print("%-10shirit." % "hi" ) #  왼쪽 정렬
print("%0.4f" % 3.141561234# 소수점 개수 표현
print("%10.4f" % 3.141561234# 전체 길이 10개, 소수점 4자리
 
# 결과값
        hi
hi        hirit.
3.1416
    3.1416
 

cs

format 함수 사용 포맷팅

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
print("I eat {0} apples".format(3))
print("Hello, Good {0} see you".format("to"))
print("{1}, Good {0} see you".format("to","Hello")) # 여러 값 넣기
print("{insa}, Good {josa} see you".format(josa = "to", insa = "Hello")) # 이름으로 넣기
print("{0:>10}".format("hi")) #  오른쪽 정렬, 총 자릿수 10
print("{0:<10}".format("hi")) # 왼쪽 정렬, 총 자릿수 10
print("{0:!^10}".format("hi")) # 가운데 정렬, 총 자릿수 10 !채우기
 
# 결과값 
I eat 3 apples
Hello, Good to see you
Hello, Good to see you
Hello, Good to see you
        hi
hi        
!!!!hi!!!!
cs


[문자열 관련 함수]

함수명

설명

len

문자열 길이 반환

count

문자열에서 해당 단어 개수 찾기, (대소문자 구분)

index or find

문자열에서 해당 단어 위치 반환, (대소문자 구분)

upper

대문자 변환

lower

소문자 변환

strip

양쪽 공백 지우기

rstrip

오른쪽 공백 지우기

lstrip

왼쪽 공백 지우기

replace

문자열 바꾸기

split

문자열 나누기

join

문자열 삽입

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 문자열 함수 
sample = "I would like to know how to use Python"
sample2 = "      Wow     "
 
print(len(sample)) # 문자열 길이 반환 
print(sample.count("i")) # 문자열에서 해당 단어 개수 찾기, 대소문자 구분 
print(sample.index("P")) # 문자열에서 해당 단어 위치 반환, 대소문자 구분
print(sample.upper()) # 대문자 변환
print(sample.lower()) # 소문자 변환
 
# 결과값
# 38
# 1
# 32
# I WOULD LIKE TO KNOW HOW TO USE PYTHON
# i would like to know how to use python
 
 
print(sample2.strip()) # 양쪽 공백 지우기
print(sample2.rstrip()) # 오른쪽 공백 지우기
print(sample2.lstrip()) # 왼쪽 공백 지우기
 
 
# 결과값
# Wow
#       Wow
# Wow     
 
print(sample.replace("Python","java")) # 첫번째 단어를 두번째 단어로 바꿔줌
print(sample.split()) # 띄어쓰기 기준으로 구분하여 리스트 반환
print(sample.split("k")) # k 기준으로 구분하여 리스트 반환
 
 
# 결과값
# I would like to know how to use java
# ['I', 'would', 'like', 'to', 'know', 'how', 'to', 'use', 'Python']
# ['I would li', 'e to ', 'now how to use Python']
 
print(",".join("abcd")) # 문자열 삽입
 
# 결과값
# a,b,c,d
cs


출처- wikidocs 점프 투 파이썬 (박응용)

+ Recent posts