Python. Functions defining and processing the beginning and the end of a string

Functions defining and processing the beginning and the end of a string


Contents


1. The function str.endswith(). Check string ending with a given suffix

The endswith() function determines whether the string ends with the specified suffix. According to the Python documentation, the general form of using a function is as follows:

f_is = s.endswith(suffix[, start[, end]])

here

  • s – source string;
  • f_is – the result of logical type. If string s ends with the suffix, then f_is = True. Otherwise f_is = False;
  • suffix – a given suffix or a tuple of suffixes that are checked for s in the string;
  • start, end – respectively, the beginning and end of the substring obtained from string s. If optional parameters start, end are specified, then a fragment of this string is considered.

Example.

# Function str.endswith()

# 1. Calling with a single suffix.
s1 = "abcdef"
f1 = s1.endswith('ef') # f1 = True

# specify a suffix
suffix = "cdef"
# search in substring 'bcd'
f2 = s1.endswith(suffix, 1, 3) # f2 = false

# search in substring 'cdef'
f3 = s1.endswith(suffix, 2, len(s1)) # f3 = True

# 2. Calling with a tuple of suffixes
# Checking if a given string ends with a tuple of suffixes
arr = ( 'ab', 'zz', 'w' ) # form a tuple

# some string
s2 = "abcdej zz"

f_is = s2.endswith(arr) # f_is = True

 

2. Function str.startswith(). Checking the beginning of a string with a given prefix

The str.startswith() function checks if the string begins with the given prefix. The general form of using the function is as follows:

f = str.startswith(prefix[, start[, end]])

here

  • f – the result of logical type. If the prefix in the string is found, then f = True (the function returns True). Otherwise f = False;
  • str – the string in which the prefix is searched;
  • prefix – specified prefix;
  • start, end – optional parameters specifying the index range of the str string that is being reviewed for the presence of a prefix. In fact, we consider a range of indices within the boundaries of [start, end-1].

Example.

# Function str.startswith() - check string prefix

# Case 1. Parameters start, end are not set
s = 'Hello world!'
f = s.startswith('He') # f = True
f = str.startswith(s, 'Hello') # f = True
f = s.startswith('abc') # f = False

# Case 2. Parameter start is set
s = 'abcde'
f = s.startswith('bcd', 1) # f = True
f = s.startswith('bcd', 2) # f = False

f = 'jklmn'.startswith('+++', 6) # f = False

# Case 3. Both start, end parameters are set
s = '0123456789'
f = s.startswith('234', 2, 5) # f = True
f = s.startswith('234', 2, 4) # f = False
f = s.startswith('234', 2, 7) # f = True

f = s.startswith('345', 1, 8) # f = False

# Case 4. Empty string, empty prefix
s = ''
f = str.startswith(s, '', 0, len(s)) # f = True


 

3. Function str.lstrip(). Return a copy of a string without the first characters specified

The str.lstrip() function returns a copy of the string in which the specified starting characters are deleted. According to the Python documentation, the general form of using a function is as follows:

s2 = s1.lstrip([chars])

where

  • s1 – the original string in which an attempt will be made to remove the initial characters from the chars set;
  • s2 – result string with deleted start characters;
  • chars – a set of characters that are treated as such that require removal. If when viewing the line s1 on the right there is a character that is not included in the set of chars, then the removal of the initial characters stops. If the chars parameter is not specified, the default character is the space character.

Example.

# Function str.lstrip() - return string without given first characters

# 1. Calling without specifying multiple characters, space is taken into account
s1 = ' abcdef '
s2 = s1.lstrip() # s2 = 'abcdef ' - deleting the first space character

s1 = '+ abcdef'
s2 = str.lstrip(s1) # s2 = '+ abcdef' - the space character is not deleted

# 2. Call specifying a set of characters
s1 = 'sdefabcdef'
s2 = s1.lstrip('defs') # s2 = 'abcdef' - deleted characters 'd','е','f','s'

s2 = '+--+-abcdef'.lstrip('-+:;') # s2 = 'abcdef'

 

4. Function str.rstrip(). Removing characters from the end of a string

The str.rstrip() function returns a copy of the string in which the specified end characters are deleted. According to the Python documentation, the general form of using a function is as follows:

s2 = s1.rstrip([chars])

where

  • s1 – the original string in which an attempt will be made to remove end characters from the chars set;
  • s2 – result string with end characters removed;
  • chars – set of characters that are treated as such that require removal. If you look at the s1 string from the end to the beginning a character that is not included in the set of chars, then the removal of the final characters stops. If the chars parameter is not specified, the default character is the space character.

Example.

# Function str.rstrip() - return a string without end specified characters

# 1. Calling a function with no parameters
s1 = 'abcdef '
s2 = s1.rstrip() # s2 = 'abcdef ' - end space characters are deleted

s1 = 'abcdef +'
s2 = str.rstrip(s1) # s2 = 'abcdef +' - the space character is not deleted

# 2. Call specifying a set of symbols
s1 = 'abcdefdfes'
s2 = s1.rstrip('defs') # s2 = 'abc' - end characters that being deleted 'd','е','f','s'

s2 = 'abcdef+--++-'.rstrip('-+:;') # s2 = 'abcdef'

 

5. Function str.strip(). Get a string with deleted start and end characters

The str.strip() function returns a copy of the string with the starting and ending characters specified. According to the Python documentation, the general form of using a function is as follows:

s2 = s1.strip([chars])

where

  • s1 – the original string that is being processed;
  • s2 – a copy string, which is the result of the function;
  • chars – the set of characters to delete. If the chars value is absent, then by default the space characters are deleted. The chars parameter is a set, so the order of the characters here does not matter. If during viewing from the beginning or from the end of a string there are characters from the chars set, then they are deleted. Characters are deleted until a character is found that is not in the chars set.

Example.

# Function str.strip() - removes given characters from the beginning and from the end of the string

# Case 1. The chars parameter is not specified - spaces are removed
s1 = ' abcd jkl '
s2 = str.strip(s1) # s2 = 'abcd jkl'

s1 = ' ' # all characters in the string are spaces
s2 = s1.strip() # s2 = '' - empty string

# Case 2. The parameter chars is specified
s1 = '+=-=+abcd+-='
s2 = s1.strip('+-=') # s2 = 'abcd'
s2 = s1.strip('=') # s2 = '+=-=+abcd+-'
s2 = '..... ,,...This is a string...'.strip(',. ') # s2 = 'This is a string'

 


Related topics