How to prevent parentheses and quotation marks from being printed when printing a string with dictionaries/lists?
When you do this:
response = '- - PLAYER used ',user, 'on the enemy. It did ',overall,' damage'
You're actually making a tuple, which is a sequence of separate values, which is why it prints that way.
Try this instead:
response = '- - PLAYER used ' + user + ' on the enemy. It did ' + str(overall) + ' damage'
How do I remove parentheses and single quotation marks from my output?
The result of split is a list. If you want the str without surrounding space, you should use strip
person = " xx "
what_he_said = " abc!"
print(f"{person.strip()}曾经说过,“{what_he_said.strip()}”")
Why this single quotes and braces in output in python?
The output you are seeing is due to the fact that you are using Python 2, but you're using the print
syntax from Python 3. In Python 3, print
is a function and takes arguments like other functions (as in print(...)
).
In Python 2, print
is a statement, and by using parentheses you are actually passing it a tuple
as its first argument (so you are printing out the Python representation of a tuple).
You can fix this in two ways.
If you add from __future__ import print_function
to the top of your file, then print
will behave like it does in Python 3.
Alternately, you can call it like:
print 'There are', length,'character'
Why Python is outputting parenthesis and quotes in my print command?
It is because print write a tuple when given more than one argument
str1 = "Hello"
str2 = "world"
print (str1, str2) #will print : (hello, world)
What you want is to
str1 = "Hello"
str2 = "world"
str3 = str1 + str2
print (str3) #will print : Hello world
Or rather
print (str1 + str2) #see how you give only one argument by concatenating str1 and str2 ?
Note though that if you want to add up two object that are not strings themselves, you will have to cast them into strings (make as if there were strings). Even if both object would print correctly alone.
It is because python can't add up two stuff that are not compatible.
x = "your picture is"
y = someimage.jpg
print(x,y) # works well and dandy, gives a tuple
print(x + y)#fails, python can t add up a string and an image
print(x + str(y))# works because str(y) is y casted into a string
Note though that not everything can be casted into a string
print() is showing quotation marks in results
You need from __future__ import print_function
.
In Python 2.x what you are doing is interpreted as printing a tuple, while you are using the 3.x syntax.
Example:
>>> name = 'Ryan'
>>> days = 3
>>> print(name, 'has been alive for', days, 'days.')
('Ryan', 'has been alive for', 3, 'days.')
>>> from __future__ import print_function
>>> print(name, 'has been alive for', days, 'days.', sep=', ')
Ryan, has been alive for, 3, days.
Related Topics
How to Check All Versions of Python Installed on Osx and Centos
How to Call a Django Function on Button Click
Python, Pandas:Write Content of Dataframe into Text File
How to Name a File by a Variable Name in Python
Python Menu-Driven Programming
What Does Sqlite3.Operationalerror: Near "-": Syntax Error Mean
Splitting One CSV into Multiple Files
Python: Read Text File and Split File into List Variables, With Each Variable Having 4 Lines Each
Why I Get Key Error Even Though Column Present in Pandas
Discord Bot Messaging a User With a Specific User Id
Converting a List into Comma Separated and Add Quotes in Python
Pandas Merge - How to Avoid Duplicating Columns
Check If a Key Exists in a Bucket in S3 Using Boto3
Calculating the Area Under a Curve Given a Set of Coordinates, Without Knowing the Function
How to Clear All Variables in the Middle of a Python Script
How to Print Float to N Decimal Places Including Trailing 0S