How do you make a string with a slash in python?
Use a raw string: Show
Note that although it looks wrong, it's actually right. There is only one backslash in the string This happens because when you just type
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:
Raw strings don't work properly when you do that. You have to use a regular string and escape your backslashes:
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
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
Video Summary
Learn More
PracticeThere 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
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.
|