0
Q:

python print advanced

# sep is between each item. empty string disables it.
print('hello', 'world', sep='')
##helloworld

# end is placed at the end of the statement. defaults to '\n' (line break)
print('The first sentence', end='. ')
print('The second sentence', end='. ')
##The first sentence. The second sentence. 

# file allows you to change where it sends the data
with open('file.txt', mode='w') as file_object:
    print('hello world', file=file_object)
## this would print "hello world" to a file named "file.txt"
# note: you can't use print for anything that isn't a character.

# use flush to disable buffering:
print(countdown, end='...', flush=True)
0
print ("Example")
0
# To print a string use quotation marks
print("Hello world!")
Hello world!

# To print a variable, use the name of the variable without quotation marks
x=10
n = "Hi!"
print(x)
10
print(n)
Hi!

# Print arguments can also be calculations
x=5
y=10
print(x+y)
15
print(y/x)
2

# To print both a variable and a string at once, use a comma (,) (this also acts as a space)
x= "Max"
print("My name is", x)
My name is Max
1
print("Hello World")
-1

New to Communities?

Join the community