Warning: session_start(): open(/tmp/sess_86412730a0d5f1ed10684ffd797f7f69, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How to remove everything before last occurrence of character in Python - LearnShareIT

How to remove everything before last occurrence of character in Python

Remove everything before last occurrence of character in Python

In this tutorial, we show you how to use the str.rsplit() method to remove everything before last occurrence of character in Python. Keep reading the information below for a better understanding.

Remove everything before last occurrence of character in Python

You can follow the steps below to be able to remove everything before the last occurrence of a character:

  1. Call the method rsplit() on your string object.
  2. Accessing the first element in the array obtained after using the method rsplit(). 
  3. Final result will be a string including everything after the last occurrence of the character.

The rsplit() function returns a list of strings after splitting them based on the given delimiters in right-to-left order. It is similar to the split() method we are familiar with but different in the order of elements in the result. 

Syntax:

string.rsplit(separator, maxsplit)

Parameters:

  • separator – Optional. By default, any space is a separator. When specifying a separator, perform a split based on that character.
  • maxsplit – Optional. The default value is -1. Specify the maximum number of times to perform the split.

To illustrate, here is an example:

message = 'Welcome-to-LearnShareIT-community'
result =message.rsplit('-', 2)
print(result)

Output:

['Welcome-to', 'LearnShareIT', 'community']

Now that you understand how to use the split() function. Let’s get started removing everything before the last occurrence of a character. Take a look at this example:

message = 'Welcome-to-LearnShareIT-community'
#Accessing the first element in the array
firstResult = message.rsplit('-', 1)[1]
print(firstResult)

#including the character in the result
secondResult = '-' + message.rsplit('-', 1)[1]
print(secondResult)  

Output:

community
-community

Okay now, let us explain to you what has just happened in the above program.

When you call the rsplit() method on the variable message, you will get an array:

firstResult = message.rsplit('-', 1)

Output:

['Welcome-to-LearnShareIT', 'community']

So to get the final result, we access the element with index ‘1’:

firstResult = message.rsplit('-', 1)[1]

Output:

community

If you want the result to include the separator character, you can simply add it to the result by string concatenation.

secondResult = '-' + message.rsplit('-', 1)[1]

Output:

-community

Summary

To sum up, you have just been shown how to use the method str.rsplit() to remove everything before last occurrence of character in Python. You can try to practice to get a better understanding of how this method works. That’s all for this tutorial. Hopefully, the information in this post is helpful to you.

Maybe you are interested:

Leave a Reply

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