I am very new to Python. I am constructing a string that is nothing but a path to the network location as follows. But it outputs the error: "Python unexpected character after line continuation character". Please help. I saw this post but I am not sure if it applies to my scenario:
syntaxerror: "unexpected character after line continuation character in python" math
s_path_publish_folder = r"\\" + s_host + "\" + s_publish_folder "\" + s_release_name
One of your \ backslashes escapes the " double quote following it. The rest of the string then ends just before the next \ backslash, and that second backslash is seen as a line-continuation character. Because there's another " right after that you get your error:
s_path_publish_folder = r"\\" + s_host + "\" + s_publish_folder "\" + s_release_name
# ^^ not end of string ||
# ^--- actual string ---^||
# line continuation /|
# extra character /
You need to double those backslashes:
s_path_publish_folder = r"\\" + s_host + "\\" + s_publish_folder "\\" + s_release_name
Better yet, use the os.path module here; for example, you could use os.path.join():
s_path_publish_folder = r"\\" + os.path.join(s_host, s_publish_folder, s_release_name)
or you could use string templating:
s_path_publish_folder = r"\\{}\{}\{}".format(s_host, s_publish_folder, s_release_name)
Related
Can't we use \t and \n in variables?
var1 = " Ryan "
print (\tvar1.strip() + \nvar1.lstrip() + \nvar1.rstrip())
I get an error:
syntax error : unexpected character after line continuation character
You need to treat them as regular characters and concatenate them properly with other strings:
print('\t' + var1.strip() + '\n' + var1.lstrip() + '\n' + var1.rstrip())
(edit from ShadowRanger's comments):
If you are on Python 3.6+, you can use f-strings instead of the + operator:
print(f"\t{var1.strip()}\n{var1.lstrip()}\n{var1.rstrip()}")
If you can't use f-strings yet, you can use the regular str.format method:
print("\t{}\n{}\n{}".format(var1.strip(), var1.lstrip(), var1.rstrip()))
You can also include them directly in literal strings:
str1 = "\tabc\ndef"
when I type this code in python it shows this error
>>> 'Ahmed' + \t 'Ashraf '
SyntaxError: unexpected character after line continuation character
what does this error mean ??
The \t escape sequence has to be inside a string literal, so the correct syntax is:
'Ahmed' + '\t' + 'Ashraf '
Outside a string literal, \ is used to indicate that you're continuing a statement on the next line, so it's called the line continuation character. It should only be followed by a newline, e.g.
>>> var1 = 'Ahmed' + \
'Ashraf'
If it's followed by some other character, such as t in your example, you get an error.
You can also do it with the format string.
>>> '%s\t%s' %('Ahmed','Ashraf')
'Ahmed\tAshraf'
>>>
Iv'e read various guides on Multi-Line Statements but cannot find a guide that has comments, variables, text and text that requires splitting over multiple lines.
I'm struggling to split the below code:
ex = 25
cmd = 'raspistill -o ' + filename + ' -t 1000 -ex ' + ex
onto a multi line with comments, like this:
cmd = 'raspistill -o ' + filename + \ # explain line 1
' -t 1000' \ # explain line 2
'-ex ' + ex # explain line 3
Is this the best way to split code over multiple lines?
You can use parentheses instead of backslashes to do line continuations:
a = ( "aaa" + # foo
"bbb" + # bar
"ccc" # baz
)
Basically when you have an expression in any kind of brackets, python will not end statements at the end of line, but will first wait until it finds the corresponding closing bracket.
I find it more readable and idiomatic than the backslashes.
I'm not sure what language you're using, but this statement is probably not being parsed the way you think:
cmd = 'raspistill -o ' + filename + \ # explain line 1
' -t 1000' \ # explain line 2
'-ex ' + ex # explain line 3
In Python, you'd get the error:
SyntaxError: unexpected character after line continuation character
The problem is that the line continuation character (backslash \) isn't escaping the newline, it's only escaping the space after it. That's because the newline doesn't follow the backslash. It doesn't come until much later, after your comment. So you still have 3 separate lines here.
Get rid of the extra comments and put them in the front, for example:
# explain lines 1, 2, and 3
#
cmd = 'raspistill -o ' + filename + \
' -t 1000' \
'-ex ' + ex
I am looking for a way to prefix strings in python with a single backslash, e.g. "]" -> "]". Since "\" is not a valid string in python, the simple
mystring = '\' + mystring
won't work. What I am currently doing is something like this:
mystring = r'\###' + mystring
mystring.replace('###','')
While this works most of the time, it is not elegant and also can cause problems for strings containing "###" or whatever the "filler" is set to. Is there a bette way of doing this?
You need to escape the backslash with a second one, to make it a literal backslash:
mystring = "\\" + mystring
Otherwise it thinks you're trying to escape the ", which in turn means you have no quote to terminate the string
Ordinarily, you can use raw string notation (r'string'), but that won't work when the backslash is the last character
The difference between print a and just a:
>>> a = 'hello'
>>> a = '\\' + a
>>> a
'\\hello'
>>> print a
\hello
Python strings have a feature called escape characters. These allow you to do special things inside as string, such as showing a quote (" or ') without closing the string you're typing
See this table
So when you typed
mystring = '\' + mystring
the \' is an escaped apostrophe, meaning that your string now has an apostrophe in it, meaning it isn't actually closed, which you can see because the rest of that line is coloured.
To type a backslash, you must escape one, which is done like this:
>>> aBackSlash = '\\'
>>> print(aBackSlash)
\
You should escape the backslash as follows:
mystring = "\\" + mystring
This is because if you do '\' it will end up escaping the second quotation. Therefore to treat the backslash literally, you must escape it.
Examples
>>> s = 'hello'
>>> s = '\\' + s
>>> print
\hello
Your case
>>> mystring = 'it actually does work'
>>> mystring = '\\' + mystring
>>> print mystring
\it actually does work
As a different way of approaching the problem, have you considered string formatting?
r'\%s' % mystring
or:
r'\{}'.format(mystring)
I am having problems with this Python program I am creating to do maths, working out and so solutions but I'm getting the syntaxerror: "unexpected character after line continuation character in python"
this is my code
print("Length between sides: "+str((length*length)*2.6)+" \ 1.5 = "+str(((length*length)*2.6)\1.5)+" Units")
My problem is with \1.5 I have tried \1.5 but it doesn't work
Using python 2.7.2
The division operator is /, not \
The backslash \ is the line continuation character the error message is talking about, and after it, only newline characters/whitespace are allowed (before the next non-whitespace continues the "interrupted" line.
print "This is a very long string that doesn't fit" + \
"on a single line"
Outside of a string, a backslash can only appear in this way. For division, you want a slash: /.
If you want to write a verbatim backslash in a string, escape it by doubling it: "\\"
In your code, you're using it twice:
print("Length between sides: " + str((length*length)*2.6) +
" \ 1.5 = " + # inside a string; treated as literal
str(((length*length)*2.6)\1.5)+ # outside a string, treated as line cont
# character, but no newline follows -> Fail
" Units")
You must press enter after continuation character
Note: Space after continuation character leads to error
cost = {"apples": [3.5, 2.4, 2.3], "bananas": [1.2, 1.8]}
0.9 * average(cost["apples"]) + \ """enter here"""
0.1 * average(cost["bananas"])
The division operator is / rather than \.
Also, the backslash has a special meaning inside a Python string. Either escape it with another backslash:
"\\ 1.5 = "`
or use a raw string
r" \ 1.5 = "
Well, what do you try to do? If you want to use division, use "/" not "\".
If it is something else, explain it in a bit more detail, please.
As the others already mentioned: the division operator is / rather than **.
If you wanna print the ** character within a string you have to escape it:
print("foo \\")
# will print: foo \
I think to print the string you wanted I think you gonna need this code:
print("Length between sides: " + str((length*length)*2.6) + " \\ 1.5 = " + str(((length*length)*2.6)/1.5) + " Units")
And this one is a more readable version of the above (using the format method):
message = "Length between sides: {0} \\ 1.5 = {1} Units"
val1 = (length * length) * 2.6
val2 = ((length * length) * 2.6) / 1.5
print(message.format(val1, val2))
This is not related to the question; just for future purpose. In my case, I got this error message when using regex. Here is my code and the correction
text = "Hey I'm Kelly, how're you and how's it going?"
import re
When I got error:
x=re.search(r'('\w+)|(\w+'\w+)', text)
The correct code:
x=re.search(r"('\w+)|(\w+'\w+)", text)
I'm meant to use double quotes after the r instead of single quotes.