Split string by forward slash in python

Split a string by backslash in Python #

Use the str.split() method to split a string on the backslashes, e.g. my_list = my_str.split('\\'). The str.split method will split the string on each occurrence of a backslash and will return a list containing the results.

Copied!

# ✅ split string on each occurrence of backslash my_str = 'one\\two\\three\\four' my_list = my_str.split('\\') print(my_list) # 👉️ ['one', 'two', 'three', 'four'] # ✅ split string on each space or backslash 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)

Notice that we had to use a second backslash to escape the first.

The backslash \ character has a special meaning in Python - it is used as an escape character (e.g. \n or \t).

By adding a second backslash, we treat the \ as a literal character.

If you have a string literal, you can also prefix it with r to mark it as a raw string.

Copied!

my_str = r'one\two\three\four' my_list = my_str.split('\\') print(my_list) # 👉️ ['one', 'two', 'three', 'four']

Strings that are prefixed with r are called raw strings and treat backslashes as literal characters.

If your string starts with or ends with a backslash, 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 backslash and another character, replace the backslash 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 backslash 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 backslash and splitting on each backslash.

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 2 forward slashes 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 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 split part of a string in Python?

Python split() method is used to split the string into chunks, and it accepts one argument called separator. A separator can be any character or a symbol. If no separators are defined, then it will split the given string and whitespace will be used by default.

How do you find the forward slash in Python?

A forward-slash (/) in a Python string can be replaced by a backslash (\) using the String replace() function, translate() method, or regular expression(re..
1 Using replace().
2 Using translate() function..
3 Using regular expression (re.sub()).
4 Backslash in Python..