Is forward slash a special character in python?

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

untitaker opened this issue

Dec 1, 2014

· 10 comments

Comments

This behavior doesn't match that of string literals anywhere else and I can't think of a reason for it.

This was referenced

Dec 1, 2014

It's not. The spec is pretty clear on which characters must be escaped, and slash is not one of them:

Any Unicode character may be used except those that must be escaped: quotation mark, backslash, and the control characters (U+0000 to U+001F).

Yes, OTOH the table with escape sequences seems to imply the opposite, or the \/ escape is a leftover.

I didn't notice this contradiction until now, but I'd argue that the table causes additional confusion, as seen in the referenced issue of a Python TOML lib.

This part of the spec is the same as JSON (http://www.ietf.org/rfc/rfc4627.txt page 4), on purpose, to make it easier for implementors to reuse code. I'm not sure why JSON has a special escape sequence for slash, but it's not a contradiction with anything else in the TOML spec (just another way you can express a slash).

The important thing is that slash may be escaped but need not be escaped. Likewise in JSON.

However, it wonder whether it wouldn't make sense to just remove the slash from that table. We would be slightly less JSON-compatible but I don't think it would hurt, while (a) apparently the current situation leads to confusion and (b) needlessly escaping the slash (as Python TOML seems to do) is just ugly.

apparently the current situation leads to confusion

Yes, that's my point.

needlessly escaping the slash (as Python TOML seems to do) is just ugly

Well, since that's against the spec, it's not really TOML's problem.

It's not really against the spec since the spec allows it.

The specification allows for the author of the file to use these escapes, but I seriously doubt that it was the intention to allow libraries escape arbitrary characters with the full unicode escape sequence.

Just a note to say that the latest master version of the spec no longer allows the \/ escape sequence and compatibility with JSON strings has been abandoned.

Use a raw string:

Nội dung chính

  • Use Forward slash / to break code into multiline code
  • Video Tutorial
  • Video Summary
  • How do you write forward slash in Python string?
  • What is the meaning of forward slash in Python?
  • What does 2 forward slashes do in Python?
  • Is forward slash a special character in Python?

>>> 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 "", 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.

Programming languages, such as Python, treat a backslash (\) as an escape character. For instance, \n represents a line feed, and \t represents a tab. When specifying a path, a forward slash (/) can be used in place of a backslash. Two backslashes can be used instead of one to avoid a syntax error. A string literal can also be used by placing the letter r before a string containing a backslash so it is interpreted correctly.

import arcpy

arcpy.GetCount_management("c:/temp/streams.shp")
arcpy.GetCount_management("c:\\temp\\streams.shp")
arcpy.GetCount_management(r"c:\temp\streams.shp")

In the following sample, backslashes are used by mistake, and \t is interpreted as a tab by Python. Get Count will fail, as the path is interpreted differently than it was intended.

import arcpy
arcpy.GetCount_management("c:\temp\streams.shp")

# ExecuteError: Failed to execute. Parameters are not valid.
# ERROR 000732: Input Rows: Dataset c:      em\streams.shp does not exist or is not supported
# Failed to execute (GetCount)
Tip:

It is possible to have a feature class and a feature dataset with the same name contained in a geodatabase. In such a case, the feature class and feature dataset will have the same catalog path. Most tools work with one or the other. However, for those tools that can work with either, such as the Copy tool, the data type can be specified to avoid ambiguity.


Feedback on this topic?

Use Forward slash / to break code into multiline code

Line break means code line change in Python, but you can use forward slash / to bluff python. You can easily break your code into multiple lines using forward slash in between.

On this page: commenting with #, multi-line strings with """ """, printing multiple objects, the backslash "\" as the escape character, '\t', '\n', '\r', and '\\'.

Video Tutorial


Python 3 Changesprint(x,y) instead of print x, y

Video Summary

  • As stated in earlier tutorials, the print() function tells Python to immediately display a given string once the command is executed. To designate a string for the print function to display, surround it in either single-quotes (' ') or double-quotes (" "). Both options are available so you can still use quotes within your string if need be. Ex: print("how are you doin' today?")
  • If the pound symbol (#) is placed before a command or any sort of string of characters, the command will appear in red and Python will ignore it during code execution. This can be used within Python to provide helpful comments to those looking at your code, or to "turn off" certain lines of code in order to test for bugs.
  • Surrounding a string with triple double-quotes (""" """) allows you to have any combination of quotes and line breaks within a string and Python will still interpret it as a single entity.

Learn More

  • You can specify multiple strings with the print() function. Just separate them out with a comma ',', and they will be printed with a space in between:
     
    >>> print('apple', 'orange', 'pear')
    apple orange pear 
    
  • In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.
     
    >>> print('apple\torange')
    apple	orange 
    >>> print('apple\norange')
    apple
    orange 
    
  • Conversely, prefixing a special character with "\" turns it into an ordinary character. This is called "escaping". For example, "\'" is the single quote character. 'It\'s raining' therefore is a valid string and equivalent to "It's raining". Likewise, '"' can be escaped: "\"hello\"" is a string begins and ends with the literal double quote character. Finally, "\" can be used to escape itself: "\\" is the literal backslash character.
     
    >>> print('It\'s raining')
    It's raining 
    >>> 'It\'s raining'          # Same string specified differently
    "It's raining" 
    >>> print("\"hello\"")
    "hello" 
    >>> print('"\\" is the backslash')   # Try with "\" instead of "\\"
    "\" is the backslash 
    
  • There are tons of handy functions that are defined on strings, called string methods. Learn about the ones on substringhood and also on case manipulation in this tutorial. This part 2 tutorial covers string methods for finding where a particular substring is located, and also for testing whether or not certain condition holds for every character.
  • Once you get comfortable with lists (upcoming), you should also check out Splitting and Joining Strings.

Practice

There are at least three ways to print I'm hugry. What are they? Try in IDLE shell.

There are at least three ways to print Fleas, Adam, Had'em (the shortest English poem ever written apparently) in three separate lines, using one print() function. What are they? Try in IDLE shell.

Explore

  • Think Python has an excellent chapter (Ch.8 Strings) devoted to strings. It gives a comprehensive overview on what one can do with this data type.

How do you write forward slash in Python string?

Programming languages, such as Python, treat a backslash (\) as an escape character. For instance, \n represents a line feed, and \t represents a tab. When specifying a path, a forward slash (/) can be used in place of a backslash.

What is the meaning of forward slash in Python?

The double forward slash in Python is known as the integer division operator. Essentially, it will divide the left by the right, and only keep the whole number component.

What does 2 forward slashes do in Python?

In Python, you use the double slash // operator to perform floor division. This // operator divides the first number by the second number and rounds the result down to the nearest integer (or whole number).

Is forward slash a special character in Python?

The documentation describes the syntax of regular expressions in Python. As you can see, the forward slash has no special function.

What does a forward slash in Python mean?

Programming languages, such as Python, treat a backslash (\) as an escape character. For instance, \n represents a line feed, and \t represents a tab. When specifying a path, a forward slash (/) can be used in place of a backslash. Two backslashes can be used instead of one to avoid a syntax error.

Is a slash a special character?

A slash. A slash symbol '/' is not a special character, but in JavaScript it is used to open and close the regexp: /...

Is forward slash a character?

The forward slash (or simply slash) character (/) is the divide symbol in programming and on calculator keyboards. For example, 10 / 7 means 10 divided by 7.

Is forward slash as escape character?

The forward slash character is used to denote the boundaries of the regular expression: ? The backslash character ( \ ) is the escaping character. It can be used to denote an escaped character, a string, literal, or one of the set of supported special characters.