Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How can I fill out a Python string with spaces?
A string is a collection of characters that can represent a single word or a whole sentence. Unlike other technologies there is no need to be declared strings in Python explicitly we can define a string with or without the datatype specifier.
A string in Python is an object of the String class, which contains multiple methods using which you can manipulate and access strings.
In this article we are going to discuss fill out a Python string with spaces. Let?s see various solutions one by one.
Using the ljust(), rjust() and center() methods
To pad the string with desired characters python provides there methods namely, ljust(), rjust() and center().
The ljust() function is used to fill out the spaces to the right of the given string or pad them.
The rjust() function is used to fill out the spaces to the left of the given string or pad them.
The center() function is used to fill out the spaces to both left and right to the string or pad them otherwise.
All these have 2 parameters ?
width ? This represents the number of spaces you want to fill. This number is including the length of the string, if the number is less than the length of the string then there won?t be any change.
fillchar(optional) ? This parameter represents a character we want to use as the padding. If nothing is specified, then the given string is padded with spaces.
Example 1
In the program given below, we are using the ljust() method for padding in the first case and we are filling with @ in the second case.
<div class="execute"></div><div class="code-mirror language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;">str1 <span class="token operator">=</span> <span class="token string">"Welcome to Tutorialspoint"</span> str2 <span class="token operator">=</span> str1<span class="token punctuation">.</span>rjust<span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span> <span class="token comment">#str3 = str1.ljust(15,'@')</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Padding the string "</span><span class="token punctuation">,</span>str1<span class="token punctuation">)</span> <span class="token keyword">print</span><span class="token punctuation">(</span>str2<span class="token punctuation">)</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Filling the spaces of the string"</span><span class="token punctuation">,</span>str1<span class="token punctuation">)</span> <span class="token keyword">print</span><span class="token punctuation">(</span>str1<span class="token punctuation">.</span>rjust<span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">,</span><span class="token string">'@'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> </div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
The output of the above program is,
('Padding the string ', 'Welcome to Tutorialspoint')
Welcome to Tutorialspoint
('Filling the spaces of the string', 'Welcome to Tutorialspoint')
Welcome to Tutorialspoint
Example 2
In the program given below, we are using rjust() method for padding in the first case and we are filling with @ in the second case.
<div class="execute"></div><div class="code-mirror language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;">str1 <span class="token operator">=</span> <span class="token string">"Welcome to Tutorialspoint"</span> str2 <span class="token operator">=</span> str1<span class="token punctuation">.</span>rjust<span class="token punctuation">(</span><span class="token number">30</span><span class="token punctuation">)</span> str3 <span class="token operator">=</span> str1<span class="token punctuation">.</span>rjust<span class="token punctuation">(</span><span class="token number">30</span><span class="token punctuation">,</span><span class="token string">'@'</span><span class="token punctuation">)</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Padding the string "</span><span class="token punctuation">,</span>str1<span class="token punctuation">)</span> <span class="token keyword">print</span><span class="token punctuation">(</span>str2<span class="token punctuation">)</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Filling the spaces of the string"</span><span class="token punctuation">,</span>str1<span class="token punctuation">)</span> <span class="token keyword">print</span><span class="token punctuation">(</span>str3<span class="token punctuation">)</span> </div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
The output of the above program is,
('Padding the string ', 'Welcome to Tutorialspoint')
Welcome to Tutorialspoint
('Filling the spaces of the string', 'Welcome to Tutorialspoint')
@@@@@Welcome to Tutorialspoint
Example 3
In the program given below, we are using the center() method for padding in the first case and we are filling with @ in the second case.
<div class="execute"></div><div class="code-mirror language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;">str1 <span class="token operator">=</span> <span class="token string">"Welcome to Tutorialspoint"</span> str2 <span class="token operator">=</span> str1<span class="token punctuation">.</span>center<span class="token punctuation">(</span><span class="token number">30</span><span class="token punctuation">)</span> str3 <span class="token operator">=</span> str1<span class="token punctuation">.</span>center<span class="token punctuation">(</span><span class="token number">30</span><span class="token punctuation">,</span><span class="token string">'@'</span><span class="token punctuation">)</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Padding the string "</span><span class="token punctuation">,</span>str1<span class="token punctuation">)</span> <span class="token keyword">print</span><span class="token punctuation">(</span>str2<span class="token punctuation">)</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Filling the spaces of the string"</span><span class="token punctuation">,</span>str1<span class="token punctuation">)</span> <span class="token keyword">print</span><span class="token punctuation">(</span>str3<span class="token punctuation">)</span> </div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
The output of the above program is,
('Padding the string ', 'Welcome to Tutorialspoint')
Welcome to Tutorialspoint
('Filling the spaces of the string', 'Welcome to Tutorialspoint')
@@Welcome to Tutorialspoint@@@
Using the format() method
We can use the string format method for filling out spaces and padding the string. We perform the format() function mostly on a print statement.
We will mention the number of spaces to be filled in a curly brace using a colon for adding the right padding. To add left padding we should also add the > symbol and for centre padding, we should use the ^ operator.
For right padding use the following statement ?
print('{:numofspaces}'.format(string))
For left padding use the following statement ?
print('{:>numofspaces}'.format(string))
For centre padding use the following statement ?
print('{:^numofspaces}'.format(string))
Example
In the example given below, we are using format method for right padding, left padding and centre padding.
<div class="execute"></div><div class="code-mirror language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;">str1 <span class="token operator">=</span> <span class="token string">"Welcome to Tutorialspoint"</span>
str2 <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token string">'{:35}'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>str1<span class="token punctuation">)</span><span class="token punctuation">)</span>
str3 <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token string">'{:>35}'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>str1<span class="token punctuation">)</span><span class="token punctuation">)</span>
str4 <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token string">'{:^35}'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>str1<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Right Padding of the string "</span><span class="token punctuation">,</span>str1<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>str2<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Left Padding of the string "</span><span class="token punctuation">,</span>str1<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>str3<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Center Padding of the string "</span><span class="token punctuation">,</span>str1<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>str4<span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
The output of the above program is,
('Right Padding of the string ', 'Welcome to Tutorialspoint')
Welcome to Tutorialspoint
('Left Padding of the string ', 'Welcome to Tutorialspoint')
Welcome to Tutorialspoint
('Center Padding of the string ', 'Welcome to Tutorialspoint')
Welcome to Tutorialspoint
