본문으로 바로가기

String Split in Python Tutorial

Learn how you can perform various operations on string using built-in Python functions like split, join and regular expressions.
2020년 6월 25일  · 2분 읽다

AI로 탐색하기

ChatGPTClaudePerplexity

Python string method split() inputs a string value and outputs a list of words contained within the string by separating or splitting the words on all the whitespaces by default. It also has an optional argument for limiting the number of splits.

The split() method converts the string into other data types like lists by breaking a string into pieces. Python provides us with two methods: .split() and .rsplit().

Both .split(), and .rsplit() take a separating element by which we are splitting the string, and a maxsplit tells us the maximum number of substrings we want.

Example of Using the .split() Method

string1

Let's split the above string using the .split() method. Notice that the .split() method starts splitting at the left of the string. It takes two arguments: separator sep and maximum splits of the string maxsplit.

string2

Example of Splitting Strings into Many Substrings

In the below example, you will split the string file into many substrings at line boundaries. Then, you will print out the resulting variable file_split. Finally, complete the for-loop to split the strings into many substrings using commas as a separator element.

substrings

When we run the above code, it produces the following result:

['mtv films election, a high school comedy, is a current example', 'from there, director steven spielberg wastes no time, taking us into the water on a midnight swim'] ['mtv films election', ' a high school comedy', ' is a current example'] ['from there', ' director steven spielberg wastes no time', ' taking us into the water on a midnight swim'].

Try it for yourself.

To learn more about string operations, please see this video from our course, Regular Expressions in Python and check out our Python Regular Expression Tutorial.

This content is taken from DataCamp’s Regular Expressions in Python course by Maria Eugenia Inzaugarat.

주제
Python

Learn more about Python

courses

Python에서의 정규 표현식

4
49.7K
문자열 조작과 정규 표현식 활용 능력 향상
자세히 보기Right Arrow
강좌 시작

courses

Python 입문

4
7M
Python을 활용한 데이터 분석 기초를 4시간 만에 마스터해 보세요. Python 인터페이스를 소개하고 많이 사용하는 대표 패키지를 함께 살펴보는 온라인 강의입니다.
더 보기Right Arrow
관련된

tutorials

Python String Tutorial

In this tutorial, you'll learn all about Python Strings: slicing and striding, manipulating and formatting them with the Formatter class, f-strings, templates and more!
Sejal Jaiswal's photo

Sejal Jaiswal

10분

tutorials

Python String Replace Tutorial

Learn to find and replace strings using regular expressions in Python.
DataCamp Team's photo

DataCamp Team

2분

tutorials

Python Concatenate Strings Tutorial

Learn various methods to concatenate strings in Python, with examples to illustrate each technique.
DataCamp Team's photo

DataCamp Team

5분

tutorials

Python String format() Tutorial

Learn about string formatting in Python.
DataCamp Team's photo

DataCamp Team

5분

tutorials

How to Split Lists in Python: Basic Examples and Advanced Methods

Learn how to split Python lists with techniques like slicing, list comprehensions, and itertools. Discover when to use each method for optimal data handling.
Allan Ouko's photo

Allan Ouko

11분

tutorials

How to Trim a String in Python: Three Different Methods

Learn the fundamentals of trimming leading and trailing characters from a string in Python.
Adel Nehme's photo

Adel Nehme

6분

더 보기더 보기