How do I eliminate extra line in Python multi-line string?

I made a HTML Basic Markup string in Python and I made the string split over multiple lines, however, I ran into a problem. This is an HTML Basic Markup string and I want to to appear like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Untitled</title>
</head>

<body>
    
</body>
</html>

So I created a string in python and this is what it looks like:

HTML_Basic_Markup = """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Untitled</title>
</head>

<body>
    
</body>
</html>
"""

When I print HTML_Basic_Markup I get an extra space at the top, so to fix this I did this:

HTML_Basic_Markup = """<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Untitled</title>
</head>

<body>
    
</body>
</html>
"""

However, I want to make the code look neat and want the Doctype to be aligned with the rest of the code, so how would I remove the line which is created at the top?

Answer

String objects support a strip method you can use to remove leading and trailing characters (including newlines). See here.

Leave a Reply

Your email address will not be published. Required fields are marked *