Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Splitting | String Methods
String Manipulation in Python

book
Splitting

One more possible task is to split the string into separate words or into separate sentences.

Surely, Python can deal with that. Use .split method with separator as an argument (space by default). It will return a list of splits. This function also has the second argument maxsplit defining the number of splits to do (by default - maximum possible). For example,

print("Python, R, SQL".split(', '))
print("Hello. My name is John. I'm 31 y/o. Nice to meet you.".split('. '))
12
print("Python, R, SQL".split(', ')) print("Hello. My name is John. I'm 31 y/o. Nice to meet you.".split('. '))
copy

By default, this method works from left to right. Like for .strip method, there is .rsplit method that works from right to left.

Tâche

Swipe to start coding

Given string libraries. Split it into the list by comma as the separator (as in example).

Solution

# Variable
libraries = "pandas, numpy, matplotlib, scipy, seaborn, keras, datetime"

# Split string
splitted_string = libraries.split(', ')
print(splitted_string)

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 4
single

single

# Variable
libraries = "pandas, numpy, matplotlib, scipy, seaborn, keras, datetime"

# Split string
splitted_string = ___.___('___')
print(splitted_string)

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

some-alt