KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Possible Duplicate: How can I print a literal “{}” characters in python string and also use .format on it? Basically, I want to use .format(), like this: my_string = '{{0}:{1}}'.format('hello', 'bonjour') And have it match: my_string = '{hello:bonjour}' #this is a string with literal curly brackets However, the first piece of code gives me an error. The curly brackets are important, because I'm using Python to communicate with a piece of software via text-based commands. I have no control over what kind of formatting the fosoftware expects, so it's crucial that I sort out all the formatting on my end. It uses curly brackets around strings to ensure that spaces in the strings are interpreted as single strings, rather than multiple arguments — much like you normally do with quotation marks in file paths, for example. I'm currently using the older method: my_string = '{%s:%s}' % ('hello', 'bonjour') Which certainly works, but .format() seems easier to read, and when I'm sending commands with five or more variables all in one string, then readability becomes a significant issue. Thanks!
Tags (comma-separated)
Save Edits
Cancel