강좌
클라우드/리눅스에 관한 강좌입니다.
프로그램 분류

파이썬기초69 : 문자열처리 내장함수1

작성자 정보

  • 관리자 작성
  • 작성일

컨텐츠 정보

본문

# ===============================================================

# #정규표현식

# # import re  를 해야 사용할 수 있다. 


# . : 문자1개의 문자와 일치

# | : 선택

# () : 하위식, 여러식을 하나로 묶을 수 있다. 

# * : 0회이상. a*b이면 b, ab, aab, aaab등이 포함된다. 

# + : 1회이상. a+b이면 ab, aab, aaab포함됨. b는 포함안됨.

# ? : 0 또는 1회. a?b는 b, ab 포함

# [] : 문자클래스 [와 ] 사이의 문자 중 하나를 선택한다. "-"기호를 사용하면 범위지정가능

#   [abc]d는 ad, bd, cd 포함됨. [a-z]는 a부터 z까지의 문자중 하나. [1-9]는 1부터 9까지의 숫자중 하나

# {m} : m회 

# {m,} : m회 이상. a{2,}b 이면 aab, aaab, aaaab 포함됨.  ab는 포함안됨.

# {m,n} : m회 이상 n회 이하. a{1,3}b이면 ab, aab, aaab는 포함됨, b, aaaab는 포함안됨.


# ===============================================================


# 문자열처리 내장함수들


print(dir(str))


#capitalize : 첫문자만 대문자로

print("PYTHON".capitalize())

Python


print("python is powerful".capitalize())

Python is powerful


#count() : 지정한 문자 갯수

print("python is powerful".count('p'))   

2

print("python is powerful".count('P'))

0

print("python is powerful".count('p', 5))

1

print("python is powerful".count('p', 0, -1))

2


#endswith() : 지정한 문자열로 끝나면 True, 아니면 False

print("python is powerful".endswith('ful'))

print("python is powerful".endswith('ful', 5))

print("python is powerful".endswith('ful', 5, -1))

print("python is powerful".endswith('m', 'l'))


#expandtabs([tabsize])  : 탭문자 \t

print("python\tis\tpowerful")

python  is      powerful

print("python\tis\tpowerful".expandtabs())

python  is      powerful

print("python\tis\tpowerful".expandtabs(1))

python is powerful

print("python\tis\tpowerful".expandtabs(3))

python   is powerful

print("python\tis\tpowerful".expandtabs(10))

python    is        powerful

print("python\tis\tpowerful".expandtabs(20))

python              is                  powerful


#find(keyword, [start, [end]])

print("python is powerful".find('p'))

0

print("python is powerful".find('p', 5, -1))

10

print("python is powerful".find('pa'))

-1


#index(keyword, [start, [end]]) : 특정문자의 위치인덱스값

print("python is powerful".index('p'))

0

print("python is powerful".index('p', 5, -1))

10

#print("python is powerful".index('pa')


#isalnum() : 숫자알파벳인가?를 확인

print("python".isalnum())

True

print("python3000".isalnum())

True

print("python3.0".isalnum())

False


#isalpha() : 알파맷으로 된 문자열인가?를 확인

print("python".isalpha())

True

print("python3000".isalpha())

False


#islower() : 소문자로된 문자열인가?를 확인

print("python".islower())

True

print("Python".islower())

False

print("python3.0".islower())

True


#isspace() : space문자가 있는가?를 확인

print("     ".isspace())

True

print("\t\n  ".isspace())

True

print("\thi\n".isspace())

False


#istitle() : 첫문자만 대문자인가?를 확인

print("python is powerful".istitle())

False

print("PYTHON IS POWERFUL".istitle())

False

print("Python Is Powerful".istitle())

True


#isupper() : 대문자인가?를 확인

print("Python".isupper())

False

print("PYTHON".isupper())

True

print("PYTHON3.0".isupper())

True


#isdecimal(), isdigit()

print("2580".isdigit())

True

print('\u0669'.isdigit())

True


#isnumeric()


#isidentifier()


#isprintable()


#join(sequence) 

print(".".join('PARK'))

P.A.R.K

print("-".join("PARK SUNG SOO"))

P-A-R-K- -S-U-N-G- -S-O-O

print("\t".join(["python", "is", "powerful"]))

python  is      powerful


#lower() : 소문자로 만들어줌.

print("PYTHON".lower())

python

print("PARK SUNG SOO".lower())

park sung soo

print("PYTHON3.0".lower())

python3.0


#lstrip([chars]) : 지정한 문자열을 없애줌

print("\t   python".lstrip())

python

print(">>> python is powerful".lstrip("> "))

python is powerful


#maketrans(x, [y, [z]])


#partition(separator) : 지정한 문자열로 잘라줌.

print("python is powerful".partition("is"))

('python ', 'is', ' powerful')


#replace(old, new, [count]) : 교체

print("python is powerful".replace("p", "P"))

Python is Powerful

print("python is powerful".replace("p", "P", 1))

Python is powerful


#rfind(keyword, [start, [end]])

print("python is powerful".rfind('p'))

10

print("python is powerful".rfind('p', 0, 9))

0

print("python is powerful".rfind('p', 2, 5))

-1

print("python is powerful".rfind('pa'))

-1


#rindex(keyword, [start, [end]])

print("python is powerful".rindex('p'))

10


#rpartition(separator) : 분리해서 튜플로 저장해줌.

print("python is powerful".rpartition("p"))

('python is ', 'p', 'owerfu


#rsplit([separator, [maxsplit]]) : 분리해서 리스트에 저장해줌

print("python is powerful".rsplit())

['python', 'is', 'powerful']

print("python is powerful".rsplit(' ', 1))

['python is', 'powerful']


#rstrip([chars]) : 지정한 문자열을 오른쪽에서만 없애줌

print("python \t".rstrip())

python

print(">>> python is powerful <<<".rstrip("<> "))

python is powerful


#split([separator, [maxsplit]]) : 지정한기호로 분리해서 리스트에 저장해줌.

print("python is powerful".split())

['python', 'is', 'powerful']

print("python is powerful".split(' ', 1))

['python', 'is powerful']


#startswith(prefix, [start, [end]]) : 지정한 문자열로 시작하는가?를 확인

print("python is powerful".startswith('py'))

True

print("python is powerful".startswith('py', 5))

False

print("python is powerful".startswith('py', 0, 5))

True

print("python is powerful".startswith(('p', 'm')))

True


#strip([chars]) : 지정한 문자를 없애줌

print("\t python \t".strip())

python

print(">>> python is powerful <<<".strip('<> '))

python is powerful


#swapcase() : 대문자는소문자로, 소문자는 대문자로

print("Python3.0".swapcase())

pYTHON3.0


#title() : 단어의 첫글자들만 대문자로 만들어줌.

print("python is powerful".title())

Python Is Powerful


#upper() : 대문자로 만들어줌.

print("Python3.0".upper())

PYTHON3.0

print("Park Sung Soo".upper())

PARK SUNG SOO




import re

batRegex =  re.compile(r'Bat(wo)?man')

print batRegex


mo1 = batRegex.search('Batwoman. The Adventures of Batman')

print mo1.group()


phoneRegex = re.compile(r'(\d\d\d-)?\d\d\d\d-\d\d\d\d')

mo1 = phoneRegex.search('My number is 010-3483-0264')

print mo1.group()

관련자료

댓글 0
등록된 댓글이 없습니다.

공지사항


뉴스광장


  • 현재 회원수 :  60,032 명
  • 현재 강좌수 :  35,773 개
  • 현재 접속자 :  185 명