Warning: session_start(): open(/tmp/sess_fbaabf8b775f85ecec8c64e1551d1ac4, 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 After A Character In A String In Python - LearnShareIT

How To Remove Everything After A Character In A String In Python

Remove everything after a character in a string in Python

Remove everything after a character in a string in Python is an interesting topic. We have found several methods. Follow the article to better understand.

Remove everything after a character in a string in Python

Use the split() function

The split() method can remove everything after a character. Based on the delimiter, it will split the string into parts.

Syntax:

str.split(sep, maxsplit)

Parameters:

  • sep: is the delimiter to split the original str string into small strings. If not specified, Python defaults sep to a blank character.
  • maxsplit: is the maximum number of splits. If not specified, Python defaults to an infinite number of splits.

Example:

  • Create a string.
  • Create a separator (set it as a unique character for easy comparison).
  • Use the split() function to split the conversion into two parts: part 1 is the characters before ‘separator’. Part 2 is the characters after ‘separator’, and only assigning part 1 to the original string from there will remove part 2.
  • Output the original string.
strEmail = "admin@learnshareit.com"
sep = '@'

# Remove characters in the string after the '@' character by the split() function
strEmail = strEmail.split(sep, 1)[0]
print(strEmail)

Output:

admin

Use Regular Expression

Regular Expression (RegEx) is a sequence of special characters that add a particular pattern, representing strings or a collection of strings.

In Python, Regular Expression is expressed through the ‘re’ module, so you have to import ‘re’ module before you want to use Regular Expression. ‘re’ Module has many methods and functions to work with RegEx, but one of the essential methods is ‘re.sub’.

The Re.sub() method will replace all pattern matches in the string with something else passed in and return the modified string.

Syntax:

re.sub(pattern, replace, string, count)

Parameters:

  • pattern: is RegEx.
  • replace: is the replacement for the resulting string that matches the pattern.
  • string: is the string to match.
  • count: is the number of replacements. Python will treat this value as 0, match, and replace all qualified strings if left blank.

Example:

  • Import re module.
  • Create a string.
  • Create a separator (set it as a unique character for easy comparison).
  • Create a pattern so that the re.sub() function looks for matching characters of the RegEx pattern that appear in ‘my_string’ and replaces those occurrences with ‘replace’. The purpose is to delete everything after a character, so I put ‘replace’ with a space.
import re

strEmail = "contact@learnshareit.com"
sep = '@'
myPattern = sep + '.*' 

# Remove characters in the string after the '@' character by the re.sub() function
strEmail = re.sub(myPattern, '', strEmail)
print(strEmail)

Output:

contact

Use the partition() method

The partition() method will return three strings:

  • String 1: characters before ‘separator.’
  • String 2: return ‘separator’.
  • String 3: characters after ‘separator’. 

We can take advantage of this to delete everything after a character.

Syntax:

partition('separator')

Example:

  • Create a string.
  • Create a separator (set it as a special character for easy comparison).
  • Use the partition() function passing in the separator.
  • Print out the string before the separator.
strEmail = "jasonwilson@learnshareit.com"
sep = '@'

# Remove characters in the string after the '@' character by the partition() function
beforeChar, char, afterChar = strEmail.partition(sep)
print(beforeChar)

Output:

jasonwilson

Summary

If you have any questions about how to remove everything after a character in a string in Python, leave a comment below. I will answer your questions. Thank you for reading!

Maybe you are interested:

Leave a Reply

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