word = "Hello"
for character in word:
print(character)
Lists and strings are somewhat similar since they are both a sequence of things. Strings are sequences of characters, but lists can be a sequence of anything. The key idea is that both strings and lists are sequences, which means that you can iterate over them with a For loop. When you iterate over a list, you get each element. When you iterate over a string, you get each character.
text = "A multi-word string!" words = text.split() print(words) text = "ONE" words = text.split() print(words) text = "" words = text.split() print(words)
Often, instead of processing a string character by character, we want to process it word by word, or by some other chunking of characters. To make this easy, strings have a method named split. Split is an awesome method because it is easy to use. When you call the method without arguments, a list of strings is returned from the string.
authors = "Alice Bob Carol"
for author in authors.split():
print("By", author)
As demonstrated here, after we have split a string, it is easy to loop over each word. In this example, we separate each author to print them individually.
desserts = "Apple Pie,Yellow Cake,Plum Tart"
print(desserts.split(","))
email = "[email protected]"
email_pieces = email.split("@")
print(email_pieces)
word = "Banana"
print(word.split("nan"))
If you do not pass anything to split, then it splits on any kind of whitespace: spaces, tabs, and new lines.
Sometimes, we want to split on other characters.
You can pass a string as an argument to split on a different character.
In this example, we split on a comma (“,”), at symbol (“@”), and the string "nan".
a_string = "AD, BE, CF"
# By each individual character
for a_character in a_string:
print(a_character)
print("----")
# On chunks of items by a separator
for an_item in a_string.split(","):
print(an_item)
print("----")
# On chunks of items by any whitespace
for a_chunk in a_string.split():
print(a_chunk)
Just to summarize, there are three major ways to iterate over a string. Without the split method, you iterate by character. With a parameter in the split method, you iterate by splitting the string on the parameter. And without a parameter in the split method, you iterate by splitting the string on whitespace.
# Get user input
user_input = input("Type numbers separated by commas: ")
# Split into parts
user_values = user_input.split(',')
# Process each part independently
total = 0
for value in user_values:
value = int(value)
total += value
print(total)
Here is an example of the split pattern in action.
The first line takes a string from the user that is expected to be separated by a specific character (a comma).
Then, the string is split into string values based on that character.
Finally, each substring is converted to an actual integer (instead of a string full of digits) and added to the total variable.
The split method is critical, because the numbers might be more than one digit in length.
split string method to turn a string into a list of strings.
split the string and then use a for loop to iterate over the resulting list of strings.