Add Spaces in Python pandas – (Add Leading, Trailing Spaces to string)

Add spaces in python with example : In this Tutorial we will learn how to add Spaces (leading, Trailing Spaces to string) in python for following Scenarios. we will also be looking at adding leading and trailing space of the column in pandas python.

  • Add Space at the start of the string in Python – rjust() function in python
  • Add space at the end of the string in Python –  ljust() function in python
  • Add white spaces at start and end of the string in python – center() function in python.
  • Add space between every letter of the string in python.
  • Add Space at the start of the column in pandas – rjust() function in pandas
  • Add space at the end of the column in pandas –  ljust() function in pandas
  • Add white spaces at start and end of the string in pandas – center() function in pandas.

 

Add Spaces in python at the start of the string:

## Add the space at Start of the string in Python

string2="Test String leading space to be added"
string_length=len(string2)+10    # will be adding 10 extra spaces
string_revised=string2.rjust(string_length)
print string_revised

In the above example we will be adding 10 extra spaces at start of the string, with the help of rjust() function.

So the output will be

‘          Test String leading space to be added’

 

 

Add spaces in python at the end of the string

## Add the space at end of the string in Python

string1="Test String Trailing space to be added"
string_length=len(string1)+10    # will be adding 10 extra spaces
string_revised=string1.ljust(string_length)
print string_revised

In the above example we will be adding 10 extra spaces at end of the string, with the help of ljust() function

So the output will be

‘Test String Trailing space to be added          ‘ 

 

 

Add white spaces at start and end of the string:

## Add the space at Start and end of the string in Python

string3="Test String leading and trailing space to be added"
string_length=len(string3)+10    # will be adding 10 extra spaces
string_revised=string3.center(string_length)
print string_revised

In the above example we will be adding 10 extra spaces,  5 at the start and 5 at end of the string, with the help of center() function.

So the output will be

‘     Test String leading and trailing space to be added     ‘

 

 

Add spaces in python between every letter of the string:

## Add the space between every letter of the string

string4="Test String"
string_revised=" ".join(string4)
print string_revised

In the above example we will be adding a space between each letter of the string. So the output will be

‘T e s t   S t r i n g’           

 

 

Add leading and trailing space of the column in pandas python:

we will be using following dataframe to depict example on adding leading and trailing space of the column in pandas python using  ljust(),rjust() and center() function in pandas. Lets first create the dataframe.

 
#### Create dataframe 

data = {'State':['Arizona AZ','Georgia GG', 'Newyork NY','Indiana IN','Florida FL'],
'Score':[62,47,55,74,31]}

df1=pd.DataFrame(data, columns=['State','Score'])
df1

so the resultant dataframe will be

Add space to the column in R 1

 

Add leading space in pandas using rjust() function : rjust() function is used to add space or padding to the left side of the specific column in pandas.

 
#### add leading space pads

df1['State_space']=df1['State'].str.rjust(13, " ")
df1

so the resultant dataframe with space padding on the left side will be

Add space to the column in R 2

 

 

 

Add trailing space in pandas using ljust() function :  ljust() function is used to add space or padding to the right side of the specific column in pandas.

 
#### add trailing space pads

df1['State_space']=df1['State'].str.ljust(13, " ")
df1

so the resultant dataframe with space padding on the right side will be

Add space to the column in R 3

 

 

Add leading and trailing space in pandas using center() function : center() function is used to add space or padding to both left and right side of the specific column in pandas.

 
#### space padding on both the sides

df1['State_space']=df1['State'].str.center(16, " ")
df1

so the resultant dataframe with space padding on either side will be

Add space to the column in R 4

 

 

previous small add spaces in python add leading and trailing space in python                                                                                                           next small add spaces in python add leading and trailing space in python