So it seems that python would convert \sto \s.
Don't confuse string representations with the actual content of the string. String representation is the way you write a string in source code, which may not exactly be the same as the string actually in memory. Backslashes are parsed specially to allow you to write non-printable characters using the backslash syntax. In this case, \s is not a valid escape sequence so the python parser interprets it literally as backslash-s. In memory, the string is still a character sequence containing the letters: `\, s
str class have a __repr__()/repr() method that returns a string that contains the source-code representation of the string, this is the string that gets printed when you don't use print statement in the REPL. This allows you to copy paste those string and reuse it in another part of the shell, but it isn't really what is stored in memory and how python interprets the string. When printing repr, python always escapes a literal backslash, this is to remove ambiguity on whether the backslash is interpreted as escape sequence or as a literal character.
Why would Python do this and what is this for? Is it the same in other languages like Java?
Most languages' string literal do interpret backslash escape sequence, although different languages treats invalid escape sequence differently. In Python, invalid backslash escape sequence is silently treated as literal backslash instead of producing an error. You'd probably encounter this kind of issue more often in Python because it has an ubiquitous repr() protocol and the default use of repr in the REPL shell.
So it seems that python would convert \sto \s.
Don't confuse string representations with the actual content of the string. String representation is the way you write a string in source code, which may not exactly be the same as the string actually in memory. Backslashes are parsed specially to allow you to write non-printable characters using the backslash syntax. In this case, \s is not a valid escape sequence so the python parser interprets it literally as backslash-s. In memory, the string is still a character sequence containing the letters: `\, s
str class have a __repr__()/repr() method that returns a string that contains the source-code representation of the string, this is the string that gets printed when you don't use print statement in the REPL. This allows you to copy paste those string and reuse it in another part of the shell, but it isn't really what is stored in memory and how python interprets the string. When printing repr, python always escapes a literal backslash, this is to remove ambiguity on whether the backslash is interpreted as escape sequence or as a literal character.
Why would Python do this and what is this for? Is it the same in other languages like Java?
Most languages' string literal do interpret backslash escape sequence, although different languages treats invalid escape sequence differently. In Python, invalid backslash escape sequence is silently treated as literal backslash instead of producing an error. You'd probably encounter this kind of issue more often in Python because it has an ubiquitous repr() protocol and the default use of repr in the REPL shell.
Python is just escaping it, so when it sees an "\" continued by a letter and if that letter doesn't have any special meaning then Python actually escapes the backslash, instead of throwing any errors.
Python interactive interface uses repr to return a string containing a printable representation of an object. So that function is adding the extra backslash to indicate that it's a literal backslash.
If you use print function to show the value of str1, you will get it printed in the stdout with just 1 backslash.
Look at this example:
str1 = '\s'
print str1
print str1.__repr__()
What does a backslash by itself ('\') mean in Python? - Stack Overflow
What does a forward slash, '/' denote when shown as a function parameter?
Slash vs backslash to separate folders
Backslash error
Videos
A backslash at the end of a line tells Python to extend the current logical line over across to the next physical line. See the Line Structure section of the Python reference documentation:
2.1.5. Explicit line joining
Two or more physical lines may be joined into logical lines using backslash characters (
\), as follows: when a physical line ends in a backslash that is not part of a string literal or comment, it is joined with the following forming a single logical line, deleting the backslash and the following end-of-line character. For example:if 1900 < year < 2100 and 1 <= month <= 12 \ and 1 <= day <= 31 and 0 <= hour < 24 \ and 0 <= minute < 60 and 0 <= second < 60: # Looks like a valid date return 1
There is also the option to use implicit line joining, by using parentheses or brackets or curly braces; Python will not end the logical line until it finds the matching closing bracket or brace for each opening bracket or brace. This is the recommended code style, the sample you found should really be written as:
if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
words_and_emoticons[i+1].lower() == "of") or
item.lower() in BOOSTER_DICT):
sentiments.append(valence)
continue
See the Python Style Guide (PEP 8) (but note the exception; some Python statements don't support (...) parenthesising so backslashes are acceptable there).
Note that Python is not the only programming language using backslashes for line continuation; bash, C and C++ preprocessor syntax, Falcon, Mathematica and Ruby also use this syntax to extend lines; see Wikipedia.
In this case, the \ is escaping the following new line character. Because Python cares about whitespace, this code is using this to allow code to be continued on a new line.
I'm using Python for a client project. I don't use Python daily, and I'm still coming up to speed with python3.
Some of the client's libraries have function definitions with a '/' as a function parameter, and I have no idea what that means. I tried searching the web, as well as the official Python docs for function definitions, and I can't find any information there, either.
Interestingly, I have noticed this character being used in the function definitions for many of Python's core objects. Shown below is an example from Python's str class
pi@auburndale:~ $ python3
Python 3.7.3 (default, Jan 22 2021, 20:04:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> help(str)
Help on class str in module builtins:
class str(object)
| str(object='') -> str
| str(bytes_or_buffer[, encoding[, errors]]) -> str
|
| Create a new string object from the given object. If encoding or
| errors is specified, then the object must expose a data buffer
| that will be decoded using the given encoding and error handler.
| Otherwise, returns the result of object.__str__() (if defined)
| or repr(object).
| encoding defaults to sys.getdefaultencoding().
| errors defaults to 'strict'.
|
| Methods defined here:
|
| __add__(self, value, /)
| Return self+value.
|
| __contains__(self, key, /)
| Return key in self.
|
| __eq__(self, value, /)
| Return self==value.
|
| __format__(self, format_spec, /)
| Return a formatted version of the string as described by format_spec.
|
| __ge__(self, value, /)
| Return self>=value.
... Can you point me to a reference where I can read more about this symbol, as well as others that might be relevant to defining Python functions?
Thanks!
This may be a stupid question, but whenever I copy a file address in Windows it copies with the folders separated by backslashes, but when I want to open the folder in python, I have to change them all to standard slashes, is there a reason for this, such as a different meaning, or is it just that they use different naming conventions?
I'm obviously not an expert, so apologies in advance if I used the wrong terminology, thank you! :)
You're being mislead by output -- the second approach you're taking actually does what you want, you just aren't believing it. :)
>>> foo = 'baz "\\"'
>>> foo
'baz "\\"'
>>> print(foo)
baz "\"
Incidentally, there's another string form which might be a bit clearer:
>>> print(r'baz "\"')
baz "\"
Use a raw string:
>>> foo = r'baz "\"'
>>> foo
'baz "\\"'
Note that although it looks wrong, it's actually right. There is only one backslash in the string foo.
This happens because when you just type foo at the prompt, python displays the result of __repr__() on the string. This leads to the following (notice only one backslash and no quotes around the printed string):
>>> foo = r'baz "\"'
>>> foo
'baz "\\"'
>>> print(foo)
baz "\"
And let's keep going because there's more backslash tricks. If you want to have a backslash at the end of the string and use the method above you'll come across a problem:
>>> foo = r'baz \'
File "<stdin>", line 1
foo = r'baz \'
^
SyntaxError: EOL while scanning single-quoted string
Raw strings don't work properly when you do that. You have to use a regular string and escape your backslashes:
>>> foo = 'baz \\'
>>> print(foo)
baz \
However, if you're working with Windows file names, you're in for some pain. What you want to do is use forward slashes and the os.path.normpath() function:
myfile = os.path.normpath('c:/folder/subfolder/file.txt')
open(myfile)
This will save a lot of escaping and hair-tearing. This page was handy when going through this a while ago.