How do you put a space after every character in python?

The Pythonic way

A very pythonic and practical way to do it is by using the string join[] method:

str.join[iterable]

The official Python documentations says:

Return a string which is the concatenation of the strings in iterable... The separator between elements is the string providing this method.

How to use it?

Remember: this is a string method.

This method will be applied to the str above, which reflects the string that will be used as separator of the items in the iterable.

Let's have some practical example!

iterable = "BINGO"
separator = " " # A whitespace character.
                # The string to which the method will be applied
separator.join[iterable]
> 'B I N G O'

In practice you would do it like this:

iterable = "BINGO"    
" ".join[iterable]
> 'B I N G O'

But remember that the argument is an iterable, like a string, list, tuple. Although the method returns a string.

iterable = ['B', 'I', 'N', 'G', 'O']    
" ".join[iterable]
> 'B I N G O'

What happens if you use a hyphen as a string instead?

iterable = ['B', 'I', 'N', 'G', 'O']    
"-".join[iterable]
> 'B-I-N-G-O'

Add spaces between the characters of a string in Python #

To add spaces between the characters of a string:

  1. Call the join[] method on a string containing a space.
  2. Pass the string as an argument to the join method.
  3. The method will return a string where the characters are separated by a space.

Copied!

my_str = 'abcde' result = ' '.join[my_str] print[result] # 👉️ 'a b c d e'

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

When called with a string argument, the join method adds the provided separator between each of the characters.

Copied!

my_str = 'abcde' result = '_'.join[my_str] print[result] # 👉️ 'a_b_c_d_e'

To insert spaces between the characters, call the join method on a string containing a space.

Copied!

my_str = 'abcde' result = ' '.join[my_str] print[result] # 👉️ 'a b c d e'

You can also add multiple spaces if you need to separate the characters by more than 1 space.

Copied!

my_str = 'abcde' result = ' '.join[my_str] print[result] # 👉️ 'a b c d e'

An alternative approach is to iterate over the string and add spaces between the characters manually.

Copied!

my_str = 'abcde' result = '' for char in my_str: result += char + ' ' * 1 result = result.strip[] print[repr[result]] # 👉️ 'a b c d e'

Note that this approach is much more inefficient than using str.join[].

You can multiply a string by a specific number to repeat the string N times.

Copied!

print[repr[' ' * 3]] # 👉️ ' ' print[repr['a' * 3]] # 👉️ 'aaa'

If you need to remove the trailing spaces after the last character, use the strip[] method.

The str.strip method returns a copy of the string with the leading and trailing whitespace removed.

The method does not change the original string, it returns a new string. Strings are immutable in Python.

In this post, we will see how to add space in Python. Specifically, we will cover the following topics:

  • Add a space between variables in print[]
  • Add spaces to the left and right sides of a string
  • Add spaces at the beginning of a string
  • Add spaces at the end of a string
  • Add spaces between characters in a string
  • Add a space between string items

So, without further ado, let’s get started.

Add a space between variables in print[]

Often, when we print variables, we want to separate them by a space to make them more readable. If variables in print[] are separated by a comma, a space gets automatically added between them. Let’s take an example.

a = 4
b = 3
print[a, b]
fname = "ashton"
lname = "agar"
print["His name is", fname, lname]

Output

4 3
His name is ashton agar

Next, we will see how to add spaces at different places in a string.

Add spaces to the left and right sides of a string

To add space to the left and right sides, we can use the center[] method. It takes the length of the returned string. Moreover, it is invoked on a string and outputs a new one. Consider the following example.

name = "ashton"
format = name.center[len[name]+8]
print[format]

Output

ashton

In the above example, the new string’s length is 14, i.e., 8 for spaces and 6 for name length. 4 spaces get added to the left and 4 to the right.

Add spaces at the beginning of a string

To add spaces at the beginning only, we can use the rjust[] method. It right aligns the given string. It takes the length of the formatted string like the center[] method does. Let’s see.

name = "ashton"
format = name.rjust[len[name]+4] #add four spaces at the beginning
print[format]

Output

ashton

Add spaces at the end of a string

The ljust[] method, on the other hand, can be used to add spaces to the end. Let’s see.

name = "ashton"
format = name.ljust[len[name]+4] #add four spaces to the end
print[format]

Output

ashton

Add spaces between characters in a string

Let’s see how to separate each character in a string by a space. For that, we can use the join[] method.

The join[] method takes an iterable [list, string] and returns a string in which all items of an iterable are joined by a separator.

Here, we will pass our input string to join[] and use space as a separator. Let’s see.

string = "cricket"
formatted = " ".join[string] #separate each character by a space
print[formatted]

Output

c r i c k e t

As you can see, we add space between each character of the string.

Add a space between string items

Using the join[] method, we can also add space between two or more string items. Let’s see.

str1 = "cricket"
str2 = "football"
str3 = "hockey"
string = " ".join[[str1, str2, str3]]
print[string]

Output

cricket football hockey

In the above example, we pass a list containing string items to the join[] method. It returns a new string having the list items joined by a space.

If you only want to display [print] strings in such a way, you can use the print[] method and separate them using a comma.

Hey guys! It’s me, Marcel, aka Maschi. I earn a full-time income online and on MaschiTuts I gladly share with you guys how I stay on top of the game! I run several highly profitable blogs & websites and love to speak about these project whenever I get a chance to do so. I do this full-time and wholeheartedly. In fact, the moment I stopped working an 8-to-5 job and finally got into online business as a digital entrepreneur, is problably one of the best decisions I ever took in my life. And I would like to make sure that YOU can get on this path as well! Don’t let anyone tell you that this can’t be done. Sky’s the limit, really…as long as you BELIEVE in it! And it all starts right here..at Maschituts!

How do you space a character in Python?

In Python, characters that are used for spacing are called as whitespace characters. They include newline, spaces, tabs, carriage return, feed, etc..
' ' – Space..
'\t' – Horizontal tab..
'\v' – Vertical tab..
'\n' – Newline..
'\r' – Carriage return..
'\f' – Feed..

How do you add a certain space in Python?

Use the multiplication operator to add a certain number of spaces to a string, e.g. result_1 = my_str + ' ' * 3 . When a character is multiplied, it gets repeated the specified number of times. Copied! The first two examples use the multiplication operator to add spaces to a string.

How do you put a space at the end of a string in Python?

Use the str. ljust[] method to add spaces to the end of a string, e.g. result = my_str. ljust[6, ' '] . The ljust method takes the total width of the string and a fill character and pads the end of the string to the specified width with the provided fill character.

What does \t do in Python?

Learn More. In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.

Chủ Đề