python 문자열 중 join() 에 대해 알아보자!

 

리스트 형태의 문자열이 있을 때, 이를 합쳐서 하나의 문자열로 만들어야 하는 경우가 있다.

다음과 같은 형태로 만들수도 있지만, 리스트의 길이가 길 경우 비효율적이다.

입력

fruit_list = ["apple", "orange", "grape"]
fruit_list[0] + " " + fruit_list[1] + " " + fruit_list[2]

출력

'apple orange grape'

 

A.join(B)는 리스트 B에 있는 모든 문자열을 하나의 단일 문자열 A로 결합한다.

입력

fruit_list = ["apple", "orange", "grape"]
" ".join(fruit_list)

출력 

'apple orange grape'

다음과 같이 여러 형태로 응용할 수 있다.

# 입력
>>> fruit_list = ["apple", "orange", "grape"]
>>> "".join(fruit_list)
# 출력
>>> 'appleorangegrape'
# 입력
fruit_list = ["apple", "orange", "grape"]
"-".join(fruit_list)
# 출력
'apple-orange-grape'

'Python > python' 카테고리의 다른 글

python library 're'를 알아보자(1)  (0) 2020.10.11

source : docs.python.org/3/library/re.html#

 

re — Regular expression operations — Python 3.9.0 documentation

This module provides regular expression matching operations similar to those found in Perl. Both patterns and strings to be searched can be Unicode strings (str) as well as 8-bit strings (bytes). However, Unicode strings and 8-bit strings cannot be mixed:

docs.python.org

텍스트 마이닝을 하고 있는데, 텍스트를 전처리 해야할일이 꽤 많아져서 라이브러리를 찾던 중 're'를 알게되었다.

정확하게 내부 함수나 요소를 알아보고 정리하기 위해서 API documents를 찾아보고 정리해보자.

 

Regular Expression Operations

 

Regular Expression Syntax

원문에 이런글이 보인다...

A regular expression (or RE) specifies a set of strings......

re는 일단 Regular Expression 의 줄임말인듯하다...

'Python > python' 카테고리의 다른 글

python 문자열 중 join() 에 대해 알아보자!  (0) 2021.06.15

+ Recent posts