What's the meaning of print(" ",end="") in python [duplicate] - python

This question already has answers here:
Meaning of end='' in the statement print("\t",end='')? [duplicate]
(2 answers)
Getting SyntaxError for print with keyword argument end=' '
(17 answers)
Closed 2 years ago.
I want to know, when I can use this expression: end="".
print('*',end="")

Normally python appends a newline to each print statement, you can replace the newline with something of your choosing with the end paramter.
>>> print('hi')
hi
>>> print('hi', end='')
hi>>> print('hi', end='bye')
hibye>>>

One of the default parameter to the print function is end = '\n'. So what that means is by default python inserts a newline right after your print statement. Most of the time this is handy and reduces having to use the newline every time. But sometimes this is not the case and we don't want it to insert a newline character in the end. So to override this default parameter we give an end argument to the print statement, and the statement will end with whatever you have provided. So in this case, we have over rode it to '', or nothing in the end, which cancels out the default newline character.

Related

How do I stop spaces appearing in this program? [duplicate]

This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 2 years ago.
In class im meant to write a program that arranges coordinates for you. I wrote this:
x = input("")
y = input("")
z = input("")
print("(",x,",",y,",",z,")\n)
and the output is: (␣0␣,␣-7831␣,␣2323␣)⤶
how do I stop the extra spaces from appearing so I get this?: (0,␣-7831,␣2323)⤶
In modern Python, the nicest way is to use an f-string:
print(f"({x},{y},{z})")
Note how the string is prefixed with f. Everything between the curly braces {} then gets interpreted as a Python expression which is subsequently converted to a string and inserted at that point.
Note that print already follows up with a newline, so unless you want an extra one (that is, a blank line), you don't need to add \n yourself.

How to run a statement again and again on a case but on different occurrences [duplicate]

This question already has answers here:
Replace characters in string from dictionary mapping
(2 answers)
Closed 2 years ago.
Hey guys I am making an encoding system in which each letter gets converted into predefined gibberish.
For example, 'a' has already been set as 'ashgahsjahjs'.
But using if a in data: print("ashgahsjahjs") executes this for one time only, if there are more than one A in the word, it would not print them with gibberish.
Using a while loop does not work either as it keeps printing indefinitely, so is there a way to print the gibberish each time there is a new occurrence of a letter.
you could try indexing the string.
your_string = "are you an apple?"
for i in range(len(your_string)):
if "a" == your_string[i]:
print("Found a at position {pos}".format(pos=i))
else:
print("Nope")

How to delete the last character in a string in python 3 [duplicate]

This question already has answers here:
Remove final character from string
(5 answers)
Closed 4 years ago.
I'm trying to delete the last character of a string, and every documentation I can find says that this works.
string = 'test'
string[:-1]
print(string)
However, whenever I try it, my IDE tells me that line two has no effect, and when I run the code it outputs "test" and not "tes", which is what I want it to do. I think that the documentation I'm reading is about python 2 and not 3, because I don't understand why else this simple code wouldn't work. Can someone show me how to remove the last letter of a string in python 3?
new_string = string[:-1]
print(new_string)
You must save the string in the memory. When we assign a variable to the string without the last character, the variable then "stores" the new value. Thus we can print it out.

Python with statement break line without backslash [duplicate]

This question already has answers here:
Python multi-line with statement
(6 answers)
Closed 5 years ago.
How can I break this line without using "\"?
with mock.patch('six.moves.builtins.open', mock.mock_open()), mock.patch('my_module.yaml.safe_load') as mock_yaml:
#do something
I tried with parenthesis but it complains with SyntaxError about the "as"
with (mock.patch('six.moves.builtins.open', mock.mock_open()),
mock.patch('my_module.yaml.safe_load') as mock_yaml):
#do something
Breaking it inside a set of parentheses, but without the parentheses around mock.patch(....), should work according to the PEP8 Python Style Guide, due to implied line continuation between parentheses:
with mock.patch('six.moves.builtins.open',
mock.mock_open()), mock.patch('my_module.yaml.safe_load') as mock_yaml:
The other option is similar to your second suggestion, but with the closing parentheses moved to before the as:
with (mock.patch('six.moves.builtins.open', mock.mock_open()),
mock.patch('my_module.yaml.safe_load')) as mock_yaml:

Why semi-colon in python after end of print and variable working? [duplicate]

This question already has answers here:
What does a semicolon do?
(5 answers)
Closed 5 years ago.
I am new to python and currently working in it.
I have write one simple program, as i read somewhere that semicolon not allowed or required in python to end statement. but i have use that and till its working fine! anyone explain me
why its possible?
here is code.
a = 10;
if a == 10:
print "value of a is %s"%(a);
else:
print "value of a is not %s"%(a);
semicolon is allowed as statement separator
>>> a=1;b=2;c=3
>>> print(a,b,c)
1 2 3
It is not required if you write each statement in a new line
Python does not require semi-colons to terminate statements. Semi-colons can be used to delimit statements if you wish to put multiple statements on the same line.

Categories