0

I am coding in Python. I have a bunch of huge multi line strings:

str = """Hello: {}
Lorem ipsum dolor sit ame.
Consectetur adipiscing elit.
Age: {}
Suspendisse turpis orci. 
Aliquam eu odio nec.
condimentum vestibulum nibh. 
Vivamus eget hendrerit urna.
Value:{}""".format("Name","6","Something")

It worked as expected.

Then I tried the real goal:

ModdedLayoutTemp = """<!DOCTYPE html>
<html>
<head>
<meta charset="uft-8">
<title>{}</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<style>
::-webkit-scrollbar { 
    /*display: none;*/
}
#Page {
    height: 100vh;
    width: 100vw;
    background-color: {};
    background-size: 100vw 100vh;
    position: fixed;
    bottom: 0;
    right: 0;
}
html {
    height: 110vh;
}
</style>
</head>
<body>
<script src="/static/viewPortScript.js"></script>
{% block body %}

{% endblock %}
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"/>
</body>
</html>
""".format("Title To Web","#ff6600")

print(ModdedLayoutTemp)

This time I got a error:

""".format("Title To Web","#ff6600") KeyError: ' \n /*display'

I have no clue why I got that error. The code is a string like the other. Does anyone have an idea of whats going wrong?

Sean Breckenridge
  • 1,932
  • 16
  • 26
Seb
  • 23
  • 9
  • You can try replacing `{` with `{{` and `}` with `}}` except for the placeholders https://stackoverflow.com/questions/5466451/how-can-i-print-literal-curly-brace-characters-in-python-string-and-also-use-fo – niraj Mar 05 '18 at 00:55

1 Answers1

1

Double the { and } characters. Python treats those as special and is expecting a key for formatting. See str.format() raises KeyError

see sharper
  • 11,505
  • 8
  • 46
  • 65
  • I tried to double up the {} where I wanted the variable. It gave me the exact same error message. I don't know If I am missing something very obvious. I double the brackets where I want the string to appear right? – Seb Mar 05 '18 at 01:08
  • The other way. The error you get is at `::-webkit-scrollbar { `. You don't want that treated as a variable so you double the braces. Same after `#Page` and `html`. – see sharper Mar 05 '18 at 01:16