writing double quotes in python

后端 未结 3 479
面向向阳花
面向向阳花 2020-12-03 17:41

I would like to write the following in a text file in the following format:

The Name is from a list of names

Item \"Name\" RollNo

e.g

Item

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-03 18:02

    With double-quote strings:

    file.write("Item \"" + Name[i] + "\" ")
    

    Or with simple quotes:

    file.write('Item "' + Name[i] + '" ')
    

    Or with triple double quotes and string interpolation:

    file.write("""Item "%s" """ % Name[i])
    

    Or with simple quotes and format:

    file.write('Item "{0}"'.format(name[i]))
    

    There are many many ways to declare string literals in Python...

提交回复
热议问题