2 분 소요

순서

  1. 문자열
  2. 이스케이프 문자

 

문자열(String) —

  • 문자를 나열한 것
  • 작은따옴표(‘), 큰따옴표(“) 사이에 문자를 넣어서 생성
  • 여러줄에 걸쳐 문자열 표현할 때는 “”” “””, ‘’’ ‘'’사이에 넣어서 생성

     

사용해보자

strA='Hello World'
print(strA)

strB='"Hello" World!'
print(strB)

출력 결과

    Hello World
    "Hello" World!

 

strC = '''Hello
World!!
abcd
'''
strD = """Hello
World!!
"""
print(strC)
print(strD)

출력 결과

    Hello
    World!!
    abcd
    
    Hello
    World!!

 

# #multi =
# """Hello
# World!!
# """
# print(multi)
#다음 라인에 이어서 명령을 사용할 때 역슬래쉬(\)를 이용

multi =\
"""Hello 
World!!
"""
print(multi)

출력 결과

    Hello 
    World!!

 

#문자열 연산

strA ="orange"
strB ="apple"

print(strA+strB)
print(strA*3)# * 는 반복 -오렌지 세번 반복

strC = "Niceman"
print('a' in strC)#a는 strA에 있다
print('z' not in strC)#z는 strC안에 없다

출력 결과

    orangeapple
    orangeorangeorange
    True
    True

 

escape String (이스케이프 문자)

 

  • \n:new line, \t :tab
print("Hello World!!\n\n")#문자열 안에서 줄을 바꿀 때 사용
print("반갑습\t\t니다.....")#문자열사이에 탭간격 맞출때

출력결과

    Hello World!!
    
    
    반갑습		니다.....
    

 

# \를 이용하여 작은따옴표와 큰따옴표 문자열 포함 시키기
escape_str1 = "Do you have a \"big cllectionn\"?"  
escape_str2 = 'whar\'s on TV?'

print(escape_str1)
print(escape_str2) 

출력결과

    Do you have a "big cllectionn"?
    whar's on TV?

 

# Raw String r'',r""

raw_str1 = r'c:\programs\python3\"'
raw_str2 = r"\"''"

print(raw_str1)
print(raw_str2)
    c:\programs\python3\"
    \"''

 

article="""Lorem Ipsum is simply dummy text of 
the printing and typesetting industry. Lorem Ipsum has been the industry's 
standard dummy text ever since the 1500s, when an unknown printer took a galley
of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic 
typesetting, remaining essentially unchanged. It was popularised 
in the 1960s with the release of Letraset sheets containing Lorem
Ipsum passages, and more recently with desktop publishing software
like Aldus PageMaker including versions of Lorem Ipsum."""
print(article.count("it"))#문자 개수 세기
print(len(article)) # len : 문자열 길이 얻어오는 함수

print(article.find("more"))#문자위치알려주기

print(article.replace("Lorem" , "로렘"))#문자바꾸기

출력결과

    3
    578
    476
    로렘 Ipsum is simply dummy text of 
    the printing and typesetting industry. 로렘 Ipsum has been the industry's 
    standard dummy text ever since the 1500s, when an unknown printer took a galley
    of type and scrambled it to make a type specimen book.
    It has survived not only five centuries, but also the leap into electronic 
    typesetting, remaining essentially unchanged. It was popularised 
    in the 1960s with the release of Letraset sheets containing 로렘
    Ipsum passages, and more recently with desktop publishing software
    like Aldus PageMaker including versions of 로렘 Ipsum.
    

 

#공백제거 
str="             hello"
print(str)

print(str .strip()) #.strip 양쪽의 공백을 지운다
print(str .lstrip()) #왼쪽 공백을 지운다 오른쪽 rstrip

출력결과

                 hello
    hello
    hello

 

str2="----nice____"
print(str2)
print(str2.strip('_'))
print(str2.strip('-'))

출력결과

    ----nice____
    ----nice
    nice____

 

print(format(123456789, ',d')) #정수만 천단위 출력
print(format(123456789, ',f')) #부동소수
print(format(123456789.1212, ',f'))

출력결과

    123,456,789
    123,456,789.000000
    123,456,789.121200

카테고리:

업데이트:

댓글남기기