How do you make a string with a slash in python?

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

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

Video Tutorial

How do you make a string with a slash in python?

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 slash a string in Python?

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. Conversely, prefixing a special character with "\" turns it into an ordinary character.

How do you make a string backslash?

If you want to include a backslash character itself, you need two backslashes or use the @ verbatim string: var s = "\\Tasks"; // or var s = @"\Tasks"; Read the MSDN documentation/C# Specification which discusses the characters that are escaped using the backslash character and the use of the verbatim string literal.

What does Slash 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).

How do you end a string with a backslash in Python?

That is, r"... \" is not a valid string literal—a raw string cannot end in an odd number of backslashes. If you need to end a raw string with a single backslash, you can use two and slice off the second.