Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Splitting and Joining Strings | String Manipulation Essentials
Working with Strings and Data Formats

bookSplitting and Joining Strings

Splitting strings and joining them back together are essential skills for working with text data in Python. The split() method lets you break a string into a list of substrings based on a specified delimiter. By default, split() uses any whitespace as the delimiter, but you can specify any character or sequence of characters as the separator.

12345678
# Splitting a comma-separated string into a list data = "apple,banana,cherry,dragonfruit" fruits = data.split(",") print("List of fruits:", fruits) # Iterating over the list of fruits for fruit in fruits: print("Fruit:", fruit)
copy

Once you have a list of strings, you can combine them back into a single string using the join() method. This method is called on the separator you want to use between each element, and takes the list as its argument. The result is a new string with the list items joined by the chosen separator.

123456789101112
# Joining a list of words with a space words = ["Data", "Science", "with", "Python"] sentence = " ".join(words) print(sentence) # Joining the same list with a hyphen hyphenated = "-".join(words) print(hyphenated) # Joining the list with no separator no_space = "".join(words) print(no_space)
copy

1. What is the default delimiter for the .split() method?

2. Which of the following are valid uses of the .join() method?

question mark

What is the default delimiter for the .split() method?

Select the correct answer

question mark

Which of the following are valid uses of the .join() method?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 3

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Awesome!

Completion rate improved to 6.67

bookSplitting and Joining Strings

Desliza para mostrar el menú

Splitting strings and joining them back together are essential skills for working with text data in Python. The split() method lets you break a string into a list of substrings based on a specified delimiter. By default, split() uses any whitespace as the delimiter, but you can specify any character or sequence of characters as the separator.

12345678
# Splitting a comma-separated string into a list data = "apple,banana,cherry,dragonfruit" fruits = data.split(",") print("List of fruits:", fruits) # Iterating over the list of fruits for fruit in fruits: print("Fruit:", fruit)
copy

Once you have a list of strings, you can combine them back into a single string using the join() method. This method is called on the separator you want to use between each element, and takes the list as its argument. The result is a new string with the list items joined by the chosen separator.

123456789101112
# Joining a list of words with a space words = ["Data", "Science", "with", "Python"] sentence = " ".join(words) print(sentence) # Joining the same list with a hyphen hyphenated = "-".join(words) print(hyphenated) # Joining the list with no separator no_space = "".join(words) print(no_space)
copy

1. What is the default delimiter for the .split() method?

2. Which of the following are valid uses of the .join() method?

question mark

What is the default delimiter for the .split() method?

Select the correct answer

question mark

Which of the following are valid uses of the .join() method?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 3
some-alt