5 Examples Unexpected Character after line Continuation Character Python

Last Updated on June 15, 2021 by admin

In this article, we will give you 5 Examples of unexpected characters after line continuation character Python.

Have you been wondering to get some excellent solutions?

5 Examples Python

Using Backslash

We write a program that calculates a person’s body mass index (BMI). To start, we need to insert height and weight into a Python program:

height = input(“What is your height? “)
weight = input(“What is your weight? “)

Note: Sublime text 3 does not support the above code. Alternative is:


height = "add your height"

weight = "add your weight."

We calculate the user’s BMI.

BMI = kg/m2

Kg” is an individual’s weight in kilograms. “m2” is the height of an individual squared. Converted into Python, the recipe to figure a BMI resembles this:

height = "10"
weight = "20"

bmi = float(weight) / (float(height) * 2)
print("Your BMI is: " + str(bmi))

We convert the upsides of “weight” and “height” to skimming point numbers so we can perform numerical capacities on them.

you then, at that point print a client’s BMI to the control center.

We convert “BMI” to a string utilizing the str(a) strategy with the goal that we can link it to the “Your BMI is: “ message.

We round the worth of “BMI” to two decimal spots utilizing the round() technique
Let’s run code:

file “main.py”, line 4
bmi = float(weight) / (float(height) * 2)

SyntaxError: unexpected character after line continuation character.

Add the file that must available in that directory.

We’ve experienced an error. This is because we have utilized “\” as the division administrator rather than the “/” sign.

We can fix our code by utilizing the “/” division administrator:

bmi = float(weight) / (float(height) * 2)
print("Your BMI is: " + str(bmi))

Code returns:

What is your height? 1.70

What is your weight? 63

Your BMI is: 18.53

Our code has successfully calculated the BMI of a user.

Line break after printing

You need to do a line break after printing something on the screen:

st ="python"
print(st, \n)
record “Add something”, line 2
print(st, \n)
^

SyntaxError: startling character after line continuation character

Simple to fix, once more—the oblique punctuation line \ goes into cites:

st = "python"
print(st, "\n")

Python
Maybe you need to add another line when printing a string on the screen, similar to this see.

Basic instance

Another basic instance of this mistake is composing the ways to Windows documents without cites. Here is another model from Stack Overflow:

F = open(C\\python\\temp\\1_copy.cp,”r”)
lines = f.readlines()
for I in lines:
fhisline = i.split(“ “)
record “Add charactor, line 1

f = open(C\\python\\temp\\1_copy.cp,”r”)
^
SyntaxError: unforeseen character after line continuation character

The full way to the record in code of line #1 should be cited “”. Additionally, a color: is to be set after the drive letter if there should be an occurrence of Windows ways:

f = open(“C:\\python\\temp\\1_copy.cp”,”r”)
lines = f.readlines()
for I in lines:
rhisline = i.split(“ “).

String connections

Frequently, you don’t have a particular record or envelope way and need to collect it from parts. You can do so through get away from successions \\ and string connections \.

Notwithstanding, this manual sorting out routinely is the justification for the unforeseen character after line continuation character mistake. In any case, the osmole to the salvage.

Utilize the path. join work.

The path. join work not exclusively does the way fruition for you, yet in addition, decides the necessary separators in the way relying upon the working framework on which you are running your program.
Import os

current_path = os.path.abspath(os.getcwd())
directory = “output”
textfile = “test.txt”
filename = os.path.join(current_path, directory, textfile).

Startling character after line continuation character

Another variety of the startling character after line continuation character blunder is the point at which you attempt to run content from the Python brief. Here is content accurately dispatched from the Windows order line:

C:\python c:\temp\helloworld.py
Hi, World!

Nonetheless, you will be taken to the Python brief on the off chance that you type python first and hit enter.

Here, you can straightforwardly run Python code like print(“Hello, World!”).

What’s more, if you attempt to show a record of the relationship with the Windows order line, you will get a blunder:

D:\temp>python

Python 3.8.2 (labels/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 Type “help”, “copyright”, “credits” or “permit” for more data.

d:\temp\helloworld.py
document “”, line 1
d:\temp\helloworld.py
^
SyntaxError: unforeseen character after line continuation character

Read More: 10 Best Python Editors for Windows, Linux & Mac

Leave a Comment