Hướng dẫn python forward slash argument

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.

Nội dung chính

  • Use Forward slash / to break code into multiline code
  • Split a string by forward slash in Python #
  • What does the forward slash mean in Python?
  • What does '/' mean in Python signature?
  • How do you pass a slash in a string in Python?
  • How do you write a forward slash?

As explained here, the / as an argument marks the end of arguments that are positional only [see here], i.e. arguments you can't use as keyword parameters. In the case of __eq__[self, value, /] the slash is at the end, which means that all arguments are marked as positional only while in the case of your __init__ only self, i.e. nothing, is positional only.

Edit: This was previously only used for built-in functions but since Python 3.8, you can use this in your own functions. The natural companion of / is * which allows to mark the beginning of keyword-only arguments. Example using both: 

# a, b are positional-only
# c, d are positional or keyword
# e, f are keyword-only
def f[a, b, /, c, d, *, e, f]:
    print[a, b, c, d, e, f]

# valid call
f[10, 20, 30, d=40, e=50, f=60]

# invalid calls:
f[10, b=20, c=30, d=40, e=50, f=60]   # b cannot be a keyword argument
f[10, 20, 30, 40, 50, f=60]           # e must be a keyword argument

Split a string by forward slash in Python #

Use the str.split[] method to split a string on the forward slashes, e.g. my_list = my_str.split['/']. The str.split method will split the string on each occurrence of a forward slash and will return a list containing the results.

Copied!

# ✅ split string on each occurrence of forward slash my_str = 'one/two/three/four' my_list = my_str.split['/'] print[my_list] # 👉️ ['one', 'two', 'three', 'four'] # ✅ split string on each space or forward slash my_str_2 = 'one two/three four five' my_list_2 = my_str_2.replace['/', ' '].split[' '] print[my_list_2] # 👉️ ['one', 'two', 'three', 'four', 'five']

The str.split[] method splits the string into a list of substrings using a delimiter.

The method takes the following 2 parameters:

NameDescription
separator Split the string into substrings on each occurrence of the separator
maxsplit At most maxsplit splits are done [optional]

If the separator is not found in the string, a list containing only 1 element is returned.

Copied!

my_str = 'one' my_list = my_str.split['/'] # 👇️ ['one'] print[my_list]

If your string starts with or ends with a forward slash, you would get empty string elements in the list.

Copied!

my_str = '/one/two/three/four/' my_list = my_str.split['/'] print[my_list] # 👉️ ['', 'one', 'two', 'three', 'four', '']

You can use the filter[] function to remove any empty strings from the list.

Copied!

my_str = '/one/two/three/four/' my_list = list[filter[None, my_str.split['/']]] print[my_list] # 👉️ ['one', 'two', 'three', 'four']

The filter function takes a function and an iterable as arguments and constructs an iterator from the elements of the iterable for which the function returns a truthy value.

If you pass None for the function argument, all falsy elements of the iterable are removed.

All values that are not truthy are considered falsy. The falsy values in Python are:

  • constants defined to be falsy: None and False.
  • 0 [zero] of any numeric type
  • empty sequences and collections: "" [empty string], [] [empty tuple], [] [empty list], {} [empty dictionary], set[] [empty set], range[0] [empty range].

Note that the filter[] function returns a filter object, so we have to use the list[] class to convert the filter object to a list.

If you need to split a string on occurrences of a forward slash and another character, replace the forward slash with the other character and split on that character.

Copied!

my_str_2 = 'one two/three four five' my_list_2 = my_str_2.replace['/', ' '].split[' '] print[my_list_2] # 👉️ ['one', 'two', 'three', 'four', 'five']

We replaced all occurrences of a forward slash with a space and split the string on each space.

You could achieve the same result by replacing each occurrence of a space with a forward slash and splitting on each forward slash.

Copied!

my_str_2 = 'one two/three four five' my_list_2 = my_str_2.replace[' ', '/'].split['/'] print[my_list_2] # 👉️ ['one', 'two', 'three', 'four', 'five']

What does the forward slash mean 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 '/' mean in Python signature?

In the case of __eq__[self, value, /] the slash is at the end, which means that all arguments are marked as positional only while in the case of your __init__ only self, i.e. nothing, is positional only.

How do you pass a slash in a string in Python?

To insert characters that are illegal in a string, use an escape character. An escape character is a backslash \ followed by the character you want to insert.

How do you write a forward slash?

There are two types of slashes: a backslash [\] and a forward slash [/]. The backslash is used only for computer coding. The forward slash, often simply referred to as a slash, is a punctuation mark used in English.

Chủ Đề