Reputation: 159
I do not understand why when inputing a space between the code with \t gives one space of line between the 'green' and 'Some things I learned so far:' output. When I use \n it gives two spaces inbetween. Shouldn't the space be the same for either \t and \n? I know that \t does tab and \n is new line. but I do not understand how \n does two spaces inbetween Code is:
fav_num = {
'rachel':'blue',
'hannah':'green',
}
print(fav_num['rachel'])
print(fav_num['hannah'])
#6-3
coding_glossary = {
'list':'mutable type where you can store info',
'tuple':'immutable type similar to list',
'string':'simple line of code'
}
print('\t')
print('Some things I learned so far: \n')
print('What a list is:')
print(coding_glossary['list'])
Output is :
blue
green
Some things I learned so far:
What a list is:
mutable type where you can store info
Process finished with exit code 0
Upvotes: 0
Views: 5440
Reputation: 5390
"\n"
character is a newline character and when you print "\n"
it sets the cursor to a new line. print
always sets a new line in the end by default. But you can change that behavior by setting the value of end
argument to an empty string.
print("hello", end="")
"\t"
is a tab
character
for i in range(20):
print("current number is\t", I)
# current number is 0
# current number is 1
# current number is 2
# current number is 3
# current number is 4
# current number is 5
# current number is 6
# current number is 7
# current number is 8
# current number is 9
# current number is 10
# current number is 11
# current number is 12
# current number is 13
# current number is 14
# current number is 15
# current number is 16
# current number is 17
# current number is 18
# current number is 19
Find more magic characters which can be useful in your programs
Upvotes: 0
Reputation: 678
By default print put a new line at the end, to modify this behavior you can set the end parameter with end=""
Example:
print("this will use 2 lines \n")
print("this will use 1 line")
print("this will use 1 line \n", end="")
Upvotes: 1
Reputation: 11
Since print() does give an '\n'
string at the end of each output the command print('\n')
gives the commandline string '\t\n'
.
For more details please see the following well documented post Link
Upvotes: 0
Reputation: 418
The statement print('\t')
is printing a tab, then returning to the next line, as the default print function automatically adds a newline. So you can't see the tab, but it is there. When you add \n to the end of the string you print, it adds a line return in addition to the default line return.
To remove the default line return, specify the 'end' parameter of the print function:
print('abcd\n', end='')
This will only include one line return.
Upvotes: 1
Reputation: 21
print by default goes to the next line. try
print(" ",end = "")
so you can see '\t' more clearly.
Also, tab jumps to the next block. A block is usually 4 spaces. Try this and notice where the . is:
print("\t", end=".\n")
print("A\t", end=".\n")
print("ABC\t", end=".\n")
print("ABCD\t", end=".\n")
Upvotes: 1
Reputation: 7558
python's built-in print function has '\n' as end character implicitly.
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
:Print objects to the text stream file, separated by sep and followed by end. sep, end, file and flush, if present, must be given as keyword arguments
So, every time you run print()
there is a '\n
' character that gets printed implicitly unless you override the behavior by passing end=
to it. (like end=''
for instance)
Upvotes: 3
Reputation: 39404
Your code can be equivalently written:
#
print()
print(‘Some things I learned so far:’)
print()
#
Upvotes: 1