简体   繁体   English

用双引号替换字符串 output 的单引号

[英]replace single quotes with double quotes for string output

I have a function databricks written in pyspark which accepts to parameters and and the values passed to the function at as below: values as below我有一个用 pyspark 编写的 function 数据块,它接受参数和传递给 function 的值,如下所示:值如下:

stringa = "XYZ_ABC"
stringb = "ab"

function is as below function 如下

def pos(stringa,stringb):
    if stringb in stringa:
      return stringa
    else:
        return stringa    
  ab = pos(stringa,stringb)

the above fuction returns me an output as 'XYZ_ABC' however the expected output is "XYZ_ABC"上面的函数返回给我一个 output 作为“XYZ_ABC”但是预期的 output 是“XYZ_ABC”

String "XYZ_ABC" or """XYZ_ABC""" or 'XYZ_ABC' or '''XYZ_ABC''' are same.字符串 "XYZ_ABC" 或 """XYZ_ABC""" 或 'XYZ_ABC' 或 '''XYZ_ABC''' 相同。

If you still want to have output on console in double-quotes use this print("""+ab+""")如果您仍想在控制台上使用双引号中的 output 使用此 print("""+ab+""")

The string doesn't actually contain the quotes, they're just printed that way because the console displays a variable the same as print(repr(ab)) .该字符串实际上并不包含引号,它们只是以这种方式打印,因为控制台显示的变量与print(repr(ab))相同。 If you print(ab) you'll see the difference.如果你print(ab)你会看到区别。

If you want quotes in the actual string you'll need to add them yourself, and you can add whichever style you like.如果您想在实际字符串中添加引号,您需要自己添加它们,并且您可以添加您喜欢的任何样式。

ab = '"' + pos(stringa,stringb) + '"'

There is no difference between using single or double-quoted strings.使用单引号或双引号字符串没有区别。 Both representations can be used interchangeably, so it has no sense to try to move from single to double-quotes.这两种表示可以互换使用,因此尝试从单引号转换为双引号是没有意义的。

If you write a double-quoted string in the console, you will see that the result is shown single-quoted.如果您在控制台中编写双引号字符串,您将看到结果显示为单引号。

>>> "asd"
'asd'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM