Double slash

Python Double Slash (//) Meaning

In this tutorial, we’ll simply learn how to use the Python Double Slash // operator. One use of this operator is to get the result of a division. The result of dividing two numbers can be an integer or a floating point number.

Now so many questions arise in mind that is “What is double slash in Python?” , “What does double slash mean in Python?” and so on.

Python Double Slash (//) Definition

In Python version 3+, both the single slash (/) and the double forward slash (//) python are used to obtain the division result, which contains a floating point number. One difference is that the single slash operator returns a correct output for the floating point result. In contrast, the double slash operator cannot return the fractional part of the floating point result. Another use of the double slash operator (//) is the internal definition of the window path value.

Python double slash operator(//)

Python Double Slash (//) Example

Let us consider a Python Program

x=5
y=4
result=x//y
print("floor division of", x, "by", y, "=", result)


The result of 5//2 is not appropriate, and the return type is an integer. The fractional part has been omitted from the output

OUTPUT

floor division of 5 by 4 = 1

We need to create a Python file with the following script to check the difference between the output of a single slash and //for division operation. In the script, 5 is defined as the divisor value, and 3 as the divisor value. The division result and the type of the result of 5/3, 5//3 will be output after executing the script

PYTHON SINGLE SLASH (/) & PYTHON DOUBLE SLASH (//)

a=3
b=4
print(a//b)
a=-3
b=4
print(a//b)


Output

 0
-1

In the above program, we are calculating the floor value with the help of a division operator called a double slash. The floor of a digit is the value that is nearest, majorly small than the actual value.  

As in the program, 3//4 is 1 and when we calculate its floor value, it will be 0. Python 2 supports a single slash division operator however we get to work with double slash since the launch of python 3. The user can simply use β€˜/’ if he wants the value in the floating number or the quotient of the floating number. Otherwise, he can use β€˜//’ to get the returned value as an integer value. In the β€˜//’ case, the factorial part will be discarded and the result will be just integer.

Python Backslash

In Python strings, β€œ\” backslash is a special character. We can also term it as the “escape” character. There are a lot of such quick functions that are defined on strings and we call them string methods. For instance, the escape character is used in representing certain whitespace characters: “\t” is a tab, “\n” is a newline, and “\r” is a carriage return.

Example

As we have a below statement where we can understand better.

txt = "We are the people of "India" also called Hindustan."


Output

This will show an error as β€œInvalid Syntax”. You can check out the error below:

File "<string>", line 1
    txt = "We are the people of "India" also called Hindustan."
                                 ^
SyntaxError: invalid syntax
> 

Example

So now, the escape character allows you to use double quotes when you normally would not be allowed:

txt = "We are the people of \β€œIndia\” also called Hindustan"
print(txt) 


Output

We are the people of "India" also called Hindustan.

Check out My Latest post on Developer Helps for some Interesting Insights


Python Single slash vs Double slash

The concept of both single slash and double slash in python comes under slash operators.

There is a concept in python called Classic Division. It means that if the operands are both integers, it will perform floor division, while for floating-point numbers, it represents true division. So single slash(/) in python is used to perform classic division whereas double slash in python (//) is used to perform the floor division. Floor division means rounding down to the nearest whole number.

Python Double slash in Path

In python, a double slash does not always work for defining paths. Path manipulation is not so easy which working with division operators. Each language has its own set of rules. Here also the use of single slash β€˜/’ and double slash β€˜//’ is made differently. So to conclude with the division operators in python, β€˜/’ is known as division operator and β€˜//’ is known as the floor division operator.

If you have any query regarding this post, please feel free to comment below.

One comment on “Python Double Slash (//) Meaning

  • Pianino Teoria , Direct link to comment

    Thank you for this very useful information. I found it quite interesting. Will definately keep close track of these pages. Great blog post. Youve gained a totally new reader. Please continue this awesome work and I anticipate see more of your superb articles. Thanks a great deal!

Leave a comment

Your email address will not be published. Required fields are marked *

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Python Online Quiz

Level up your coding skills with our interactive programming quiz!