0
string='98.87,100.91,22.12'
print(string)
98.87,100.91,22.12

then I want to add apostrophe like this

'98.87','100.91','22.12'

how can I do this?

thanks


Thanks all

I know I can use "'98.87'" to print

'98.87'

But actually I want to add apostrophe by code

Because I will get string from

string = request['string']

then I want to let this string in SQL query

SELECT * FROM table WHERE str IN ({}).format(string)

I wnat to result become to

SELECT * FROM table WHERE str IN ('98.87','100.91','22.12')
disney82231
  • 189
  • 1
  • 3
  • 11
  • 2
    Does this answer your question? [How to convert list into string with quotes in python](https://stackoverflow.com/questions/16054870/how-to-convert-list-into-string-with-quotes-in-python) / [Join a list of strings in python and wrap each string in quotation marks](https://stackoverflow.com/questions/12007686/join-a-list-of-strings-in-python-and-wrap-each-string-in-quotation-marks) (also use `str.split(',')` to make your string into a list first) – TrebledJ May 07 '20 at 03:36
  • I updated my answer below to add the apostrophe for each item in a list using code into a string including those apostrophes. I hope it helped. – K.Land_bioinfo May 07 '20 at 03:51

3 Answers3

1
numbers=[98.87,100.91,22.12]
mylist= []
for num in numbers:
    a= "'%s'" %(num)
    mylist.append(a)

mylist2=",".join(mylist)

print(mylist2)
SherylHohman
  • 16,580
  • 17
  • 88
  • 94
K.Land_bioinfo
  • 170
  • 1
  • 3
  • 12
  • 1
    @aviboy2006, what did you edit here? – K.Land_bioinfo May 07 '20 at 16:02
  • Formatting and text – Avinash Dalvi May 07 '20 at 16:03
  • 1
    What was wrong with my formatting and text? – K.Land_bioinfo May 07 '20 at 16:04
  • Add label or text and python format – Avinash Dalvi May 07 '20 at 16:27
  • 1
    OK, I see. However, my original answer was in python format (4 space indent under the for loop statement). I don't believe your extra space indentations following the numbers list is necessary. Am I wrong? Just asking for my own knowledge since I am still fairly new at programming and Python. – K.Land_bioinfo May 07 '20 at 16:41
  • 2
    @aviboy2006 indention is 1 of 2 proper methods to apply code formatting. SO *automatically* applies python language hints here, as the question is tagged as a python question. It is absolutely NOT necessary to manually, explicitly, tag the code in most cases. It's only necessary to manually add language hints when the code from technology associated with tags the Q/A is tagged with, *and* the auto detection scheme gets it wrong. **Superfluous**, edits *should be rejected* when they appear in the Review Queue. Points should NOT be gained for superfluous edits, or edits that *miss* actual issues – SherylHohman May 07 '20 at 16:51
  • 1
    Ok apology for this. User can revert changes – Avinash Dalvi May 07 '20 at 16:52
  • 2
    While this code may solve the OP's issue, it is better to include an explanation as to why this solution works. This enables both the OP and future visitors learn, so they can apply this knowledge to their own applications. It also keeps SO quality high. High quality answers are much more likely to be upvoted. – SherylHohman May 07 '20 at 17:01
1

You can use double quotes to define your string and the single quotes inside it or use backslash ('\')

string_value = "'98.87','100.91','22.12'"

Or

string_value = '\'98.87\',\'100.91\',\'22.12\''

To create a new string with the apostrophes you could do:

string = '98.87,100.91,22.12'
string_list = [f"'{value}'" for value in string.split(',')]
separator = ','
string_with_apostrophe = separator.join(string_list)
arthursribeiro
  • 326
  • 3
  • 9
0

This should work with any numbers:

>>> a = '98.87,100.91,22.12'
>>> nums = a.split(',')
>>> nums
['98.87', '100.91', '22.12']
>>> result = str(nums).replace('[','').replace(']','')
>>> result
"'98.87', '100.91', '22.12'"
>>> 

You may easily clean up spaces using replace(), which I did not for the sake of simplicity.

lenik
  • 23,228
  • 4
  • 34
  • 43