Edu/01. Language: Python

데이터 타입: 문자열

Lacuna028 2021. 11. 30. 11:44

- 문자열

- 문자열 연산

- 문자열 슬라이싱

 

# 문자열 생성
str1 = "I am Boy."
 
 
# 문자열 출력
print(type(str1))
 
 
# 문자열 길이
print(len(str1))
 
 
# 빈 문자열
str_t1 = ''
str_t2 = str()
 
 
# 이스케이프 문자 사용
escape_str1 = "Do you have a \"big collection\"?"
multi_str2 = \
    '''
    문자열 멀티라인
    역슬래시(\) \
    테스트
    '''
t_s1 = "Tab \tClick!"
raw_s1 = r'C:\Programs\python3\"'
 
 

# 문자열 연산: +, *

+를 사용하여 문자열끼리 붙이기 가능

*를 사용하여 문자열을 특정 횟수만큼 반복 가능

 

 

# in, not in 을 사용하여 불린형으로 반환

str_o1 = "Niceman"

print('x' in str_o1) # output: False
print('e' not in str_o1) # output: False

 

# 문자열 형변환

print(str(77))  # type 확인
print(str(10.4))
print(str(True))
print(str(complex(12)))

 

# 문자열 내장 함수

capitalize() Converts the first character to upper case
첫 번째 문자를 대문자로 변환합니다.
casefold() Converts string into lower case
문자열을 소문자로 변환합니다.
center() Returns a centered string
가운데에 있는 문자열을 반환합니다.
count() Returns the number of times a specified value occurs in a string
문자열에서 지정한 값이 발생하는 횟수를 반환합니다.
encode() Returns an encoded version of the string
인코딩된 문자열 버전을 반환합니다.
endswith() Returns true if the string ends with the specified value
expandtabs() Sets the tab size of the string
find() Searches the string for a specified value and returns the position of where it was found
format() Formats specified values in a string
format_map() Formats specified values in a string
index() Searches the string for a specified value and returns the position of where it was found
isalnum() Returns True if all characters in the string are alphanumeric
isalpha() Returns True if all characters in the string are in the alphabet
isascii() Returns True if all characters in the string are ascii characters
isdecimal() Returns True if all characters in the string are decimals
isdigit() Returns True if all characters in the string are digits
isidentifier() Returns True if the string is an identifier
islower() Returns True if all characters in the string are lower case
isnumeric() Returns True if all characters in the string are numeric
isprintable() Returns True if all characters in the string are printable
isspace() Returns True if all characters in the string are whitespaces
istitle() Returns True if the string follows the rules of a title
isupper() Returns True if all characters in the string are upper case
join() Converts the elements of an iterable into a string
ljust() Returns a left justified version of the string
lower() Converts a string into lower case
lstrip() Returns a left trim version of the string
maketrans() Returns a translation table to be used in translations
partition() Returns a tuple where the string is parted into three parts
replace() Returns a string where a specified value is replaced with a specified value
rfind() Searches the string for a specified value and returns the last position of where it was found
rindex() Searches the string for a specified value and returns the last position of where it was found
rjust() Returns a right justified version of the string
rpartition() Returns a tuple where the string is parted into three parts
rsplit() Splits the string at the specified separator, and returns a list
rstrip() Returns a right trim version of the string
split() Splits the string at the specified separator, and returns a list
splitlines() Splits the string at line breaks and returns a list
startswith() Returns true if the string starts with the specified value
strip() Returns a trimmed version of the string
swapcase() Swaps cases, lower case becomes upper case and vice versa
title() Converts the first character of each word to upper case
translate() Returns a translated string
upper() Converts a string into upper case
zfill() Fills the string with a specified number of 0 values at the beginning

(출처: https://www.w3schools.com/python/python_ref_string.asp)

 

 

# 문자열 수정

# 문자열 상태에서는 수정이 불가능(immutable)

im_str = "Hello world"
im_str[0] = "T"  # 수정 불가
 
 
# 슬라이싱
print(str_sl[0:3])
print(str_sl[:len(str_sl)])
print(str_sl[:len(str_sl) - 1])
print(str_sl[:])
print(str_sl[1:])
print(str_sl[1:4:2])
print(str_sl[-3:6])
print(str_sl[1:-2])
print(str_sl[::-1])
print(str_sl[::2])

 

# 문자열 삭제

del str_sl
 
 
# 아스키코드
a = 't'

print(ord(a))
print(chr(116))