Problem Formulation and Solution Overview
Method 1: Use replace()
This example uses Python’s replace() method to place a single space character between the fields.
employee = 'James,Smith,Teacher,jsmith@acme.org,Toronto,Canada'
spaced = employee.replace(',', ' ')
print(spaced)The above code declares a comma-delimited row containing an employee’s data. This data saves to employee.
Next, replace() is appended to employee and two (2) arguments are passed: the string to replace (',') and the string to replace it with (' '). The results save to spaced and are output to the terminal.
James Smith Teacher jsmith@acme.org Toronto Canada |
Method 2: Use Multiplication Operator
This example uses the multiplication operator to display x number of spaces between fields (' '*x).
employee = 'James,Smith,Teacher,jsmith@acme.org,Toronto,Canada'
spaced = employee.replace(',', ' '*5)
print(spaced)The above code declares a comma-delimited row containing an employee’s data. This data saves to employee.
Next, replace() is appended to employee and two (2) arguments are passed:
- The string to replace (
','). - The string to replace the above with. Which, in this case, is the space (
' ') character multiplied by the number of occurrences (' '*5).
The results save to spaced and are output to the terminal.
James Smith Teacher jsmith@acme.org Toronto Canada |
Method 3: Use f string and chr()
This example uses Python’s f-string and chr() to print multiple spaces between two variables.
fname = 'James'
lname = 'Smith'
print(f'{fname} {chr(0x20)*10} {lname}')The above code declares two (2) variables, fname and lname.
The following line uses the f-string inside the print statement to do the following.
- Pass the variable
fname. - Pass the
chr(0x20)function, which resolves to the space character and uses multiplication to display this character 10 times. - Pass the variable
lname.
The results are output to the terminal.
James Smith |
Method 4: Use split() and join()
This example uses split() and join() to print multiple spaces between a split string.
employee = 'James Smith Teacher'
print(f'{chr(0x20)*10}'.join(employee.split()))The above code saves an employee’s data to employee.
The following line uses the f-string in the print statement and does the following.
- Calls
join()and is passed one (1) argument, the string to split. By default, the argument will split on the space (‘ ‘) character unless otherwise specified. - Next, the
chr()function is called and passed the space character. This character is multiplied by 10 (chr(0x20)*10). - The results are 10 spaces between each word in
employee.
The results are output to the terminal.
James Smith Teacher |
Method 5: Use %s
This example uses the %s format string and the multiplication operator to create spaces between each word.
print("%sJames %sSmith" % (' '*5, ' '*3))The above code does the following.
- Defines two (2)
%sformat strings. This lets Python know to expect two (2) strings. - Next, the % operator indicates to Python to expect two (2) arguments, one for each format string which defines the spacing between each string (
(' '5, ' '3)).
The results are output to the terminal, and we get five (5) spaces before James and three (3) spaces before Smith.
James Smith |
Summary
This article has provided five (5) ways to print spaces to select the best fit for your coding requirements.
Good Luck & Happy Coding!
Programming Humor
