Probably an easy question that I couldn't quite find answer to before...
I'm formatting a table [in text] to look like this:
Timestamp: Word Number
The number of characters between the : after timestamp and the beginning of Number is to be 20, including those in the Word [so it stays aligned]. Using python I've done this:
offset = 20 - len[word]
printer = timestamp + ' ' + word
for i in range[0, offset]:
printer += ' '
printer += score
Which works, but python throws an error at me that i is never used ['cause it's not]. While it's not a huge deal, I'm just wondering if there's a better way to do so.
Edit:
Since I can't add an answer to this [as it's marked duplicate] the better way to replace this whole thing is
printer = timestamp + ' ' + word.ljust[20] + score
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
In this article, we will learn about how to print space or multiple spaces in the Python programming language. Spacing in Python language is quite simple than other programming language. In C languages, to print 10 spaces a loop is used while in python no loop is used to print number of spaces.
Following are the example of the print spaces:
Example 1: A simple way to print spaces
Python3
print
[
"GeeksForGeeks"
]
print
[
' '
]
print
[
" "
]
print
[
"Geeks For Geeks"
]
Output:
GeeksForGeeks Geeks For Geeks
Example 2: Printing spaces between two values while printing in a single print statement.
Python3
x
=
1
y
=
2
print
[
"x:"
,x]
print
[
"y:"
,y]
print
[x,
"+"
,y,
"="
,x
+
y]
Output:
x: 1 y: 2 1 + 2 = 3
Example 3: Print multiple spaces between two values.
Python3
print
[
"Geeks"
+
" "
+
"For"
+
" "
+
"Geeks"
]
print
[
"Geeks"
,
"For"
,
"Geeks"
]
print
[
"Geeks"
+
" "
*
3
+
"For"
+
" "
*
3
+
"Geeks"
]
print
[
"Geeks"
+
" "
*
5
+
"For"
+
" "
*
10
+
"Geeks"
]
Output:
Geeks For Geeks Geeks For Geeks Geeks For Geeks Geeks For Geeks
Add a certain number of spaces to a String 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!
my_str = 'abc' # ✅ add spaces to end of string result_1 = my_str + ' ' * 3 print[repr[result_1]] # 👉️ 'abc ' # ✅ add spaces to beginning of string result_2 = ' ' * 3 + my_str print[repr[result_2]] # 👉️ ' abc' # # ✅ pad end of string with spaces result_3 = my_str.ljust[6, ' '] print[repr[result_3]] # 👉️ 'abc ' # ✅ pad beginning of string with spaces result_4 = my_str.rjust[6, ' '] print[repr[result_4]] # 👉️ ' abc' # ✅ add spaces between the characters of a string result_5 = ' '.join[my_str] print[repr[result_5]] # 👉️ 'a b c' # ✅ add spaces in the middle of a string my_str_2 = 'helloworld' idx = my_str_2.index['w'] result_6 = my_str_2[0:idx] + ' ' * 3 + my_str_2[idx:] print[repr[result_6]] # 👉️ 'hello world'
The first two examples use the multiplication operator to add spaces to a string.
Copied!
my_str = 'abc' result_1 = my_str + ' ' * 3 print[repr[result_1]] # 👉️ 'abc ' result_2 = ' ' * 3 + my_str print[repr[result_2]] # 👉️ ' abc'
When a character is multiplied, it gets repeated the specified number of times.
Copied!
print[repr[' ' * 3]] # 👉️ ' ' print['a' * 3] # 👉️ 'aaa'
You can also use the str.ljust
and str.rjust
methods to pad the string with spaces to a specified width.
Copied!
my_str = 'abc' result_3 = my_str.ljust[6, ' '] print[repr[result_3]] # 👉️ 'abc ' result_4 = my_str.rjust[6, ' '] print[repr[result_4]] # 👉️ ' abc'
The str.ljust
[left justify] and str.rjust
[right justify] methods take the total width of the string and a fill character as arguments and pad the string to the
specified width with the provided fill character.
If you need to add spaces between the characters of a string, use the join[]
method.
Copied!
my_str = 'abc' result_5 = ' '.join[my_str] print[repr[result_5]] # 👉️ 'a b c'
The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.
If you need to add spaces in the middle of a string, use string slicing.
Copied!
my_str_2 = 'helloworld' idx = my_str_2.index['w'] result_6 = my_str_2[0:idx] + ' ' * 3 + my_str_2[idx:] print[repr[result_6]] # 👉️ 'hello world'
The syntax for string slicing is my_str[start:stop:step]
.
The start
value is inclusive, and the stop
value is exclusive [up to, but not including].