Home > Back-end >  How can I include both quotes and double quotes in a text string in python?
How can I include both quotes and double quotes in a text string in python?

Time:03-13

I am trying to automate a Minecraft command that contains both quotes " and '. When I connect the string variables they make a mess with the quotes that start and finish the string. Is there a way to avoid that?

Name = input("User name: ")
Age = input("User age: ")
Sex = input("User gender: ")
Job = input("User job: ")
Final = '/give @s flower_banner_pattern{display:{Lore:['{"text":"Name: "   Name   ","color":"white"}','{"text":"Age: "   Age   ','color":"white"}','{"text":"Sex: "   Sex   ","color":"white"}','{"text":"Job: "   Job   ','color":"white"}']}}'

Do you have any ideas? Thanks for the help!

[sorry for my English I'm italian]

CodePudding user response:

to indicate quotes and double quotes in python you have to indicate them in the following way:

\'-> Single quote for python string

\" -> Two quotes for python String

This that I just indicated is only to place those characters in the string, they have nothing to do with the quotes at the beginning and end of the string

CodePudding user response:

Using ' and " simultaneously sometimes would be erroneous. If you intend to use both single and double quotations simultaneously and also multiple quotations, I suggest you use \ to separate the main and the major quotations. The following example would give you insight into the instruction:

myString = 'there is a few quotations such as this \' and this \'. There is no need to use slash with ".'
print(myString)

Output

there is a few quotations such as this ' and this '. There is no need to use slash with ".

CodePudding user response:

I do not know how Minecraft commands work, but if you start the Final variable with a single quote ', then in the middle of the string if you want yto use a single quote, you should do \'.

This means that the following should work:

Name = input("User name: ")
Age = input("User age: ")
Sex = input("User gender: ")
Job = input("User job: ")
Final = '/give @s flower_banner_pattern{display:{Lore:[\'{"text":"Name: "   Name   ","color":"white"}\',\'{"text":"Age: "   Age   \',\'color":"white"}\',\'{"text":"Sex: "   Sex   ","color":"white"}\',\'{"text":"Job: "   Job   \',\'color":"white"}\']}}'

  • Related