How to Print the Characters in a String Separated By Space

In this Python tutorial, I will show you how to print the characters in a string separated by space.

While analyzing the text, I had to split the string into characters separated by space, so I used the for loop. Additionally, I needed to split the string by space, so I used the split() function.

In this tutorial, I have explained both approaches to splitting a string into characters and words by space. I have also explained how to use a regular expression to split the string by space.

Print the Characters in a String Separated By Space Using the For loop

You can use the for loop to print the characters in a string separated by spaces. The logic is straightforward: You must iterate over the string and then print each character separated by space using the Python print() function.

For example, if you have the string ‘Australia’, understand and execute the code below to print each character separated by a space.

str = 'Austrailia'

for i in range(0, len(str)):  
    print(str[i], end="  ");  
Print the Characters in a String Separated By Space Using the For loop

From the output, you can see the string ‘Australia’ is divided into characters, and each character is separated by space.

Here, the loop ‘for i in range(0, len(str))’ runs from index 0 to the length of the string. Where ‘i’ represents the index of the characters in the string, this line ‘print(str[i], end=” “)’ prints each character separated by a space.

READ:  Python Tkinter Events

The str[i] represents the character at the index ‘i’, then end=” “ adds the space after printing each character. Thus, it prints ‘A u s t r a i l i a’ separated by space. This is how to use the for loop to print the characters in a string separated by space.

Here, you have learned how to print the characters of a string separated by space. Sometimes, you must split the string by space; let’s see that in the next section.

Python Split String By Space

The two most commonly used approaches to split pythons string by space are the split() method and the regular expression.

Python String Split By Space Using Split() Method

Python has a built-in function called split(), which splits the given string into a list of words based on the specified separator.

The syntax is given below.

str.split(separator, maxsplit)

Where,

  • separator: It is the delimiter where the string is split. If not provided, white space is the default separator.
  • maxsplit: It is an optional argument that specifies how many splits to perform. If not provided or -1, it splits at all occurrences.

For example, suppose you have an address in a string like “1600 Pennsylvania Ave NW, Washington, DC 20500”. You want split this address word by word, for that you can use here split() method as shown in the code below.

address = "1600 Pennsylvania Ave NW, Washington, DC 20500"
address.split()
Python String Split By Space using Split() Method

Look, the whole address is divided into a list of words, and it is divided based on space because when you call something like this address.split(), you are calling it without specifying a separator.

READ:  How to convert a tuple to a comma separated string in Python [4 Methods]

By default, the separator is considered a whitespace when not provided, so the string is divided into a list of words separated by space.

Here, we aim to split the string by space, so we will not see how to split the string by a different separator.

Next, let’s see how to split string by space using the regular expression.

Python String Split By Space Using Regular Expressions

First, the regular expression is also called regexp, which consists of a sequence of characters that act as a pattern for searching or manipulating text data.

In Python, a module called re provides a function called split(), not the Python built-in function. The re module‚Äôs split() function can handle more complex string-splitting requirements. It allows for regular expression pattern matching.

For example, you have string landmarks like “Statue of Liberty\tGrand Canyon\nMount Rushmore”. As shown in the code below, you can specify the regular expression to split string by space within the split() function.

import re

landmarks = "Statue   of Liberty\tGrand Canyon\nMount Rushmore"
re.split('\s+', landmarks)
Python String Split By Space Using Regular Expressions

In the above example, \s+ is a Python regular expression that matches any whitespace character (spaces, tabs, line breaks). So it splits the string into list words based on the space, or you can see the string even contains the tab ‘\t’ and line breaks ‘\n’.

This is how to use a regular expression to split the string by space using the split() function of the re-module.

Conclusion

In this Python tutorial, you learned how to print the characters in a string separated by space using the for loop and Python built-in split function.

READ:  Stacked Bar Chart Matplotlib - complete tutorial

Additionally, you learned how to split the string using the split() function of the re-module.

You may like to read: