Follow

Follow

Usage of forward slash (/) in Python

What are the various ways forward slash can be used in Python programming

Soumendra kumar sahoo's photo
Soumendra kumar sahoo
ยทDec 5, 2022ยท

1 min read

Usage of forward slash (/) in Python
Play this article

Table of contents

  • Division
  • Floor division
  • Paths
  • Regular expressions

In Python, the forward-slash (/) has several different uses depending on the context in which it appears. Some of the main uses of the forward slash in Python include:

Division

When used between two numeric values, the forward slash performs division. For example, 10 / 3 evaluates to 3.3333333333333335.

>>> 10/3
3.3333333333333335

Floor division

When used between two numeric values, the forward slash followed by another forward slash (//) performs floor division, which rounds the result down to the nearest integer. For example, 10 / 3 evaluates to 3, and 10 // 3 evaluates to 3.

>>> 10//3
3

Paths

In strings, the forward slash is often used as a separator in file paths or URLs. For example:

path = "C:/Users/john/Documents/file.txt"
url = "https://www.example.com/path/to/resource"

Regular expressions

In regular expressions, the forward slash is often used to escape special characters. For example:

import re

pattern = r"\d+/\d+/\d+"
text = "The date is 01/01/2021"
match = re.search(pattern, text)

Thanks for reading, you can further check:

Let me know what you think at any of the following places:

ย 
Share this