Python. Built-in functions for working with strings len(), max(), min()




Built-in functions for working with strings len(), max(), min()


Contents


Search other websites:

1. Built-in len() function. The length of the string

In Python, the built-in len() function is always available. The function determines the number of items in the object. In the case of strings, the function returns the number of characters in the string.

The general form of using the function is as follows:

count = len(s)

where

  • s – some string of characters;
  • count – the number of characters in string s.

Example.

# Function str.len() - the length of a string
s = 'bestprog.net'
d = len(s) # d = 12

# Empty string
d = len('') # d = 0

 

2. Function min(). Defining of an item with a minimum code per string

The built-in min() function can be applied to objects of various types, including strings. The function finds the smallest element in the sequence.

In the case of strings, the min() operation can be applied in one of two aspects:

  • for one string. In this case, the character that has the smallest code is selected in the string. This character is the result of the min() function;
  • for multiple strings. In this case, a string is selected which, in lexicographic order, follows the first alphabetically.

According to the Python documentation, the general form of using a function for strings is as follows:

res_str = min(s)

or

res_str = min(s1[, s2[, ... sn]])

here

  • s, s1, s2, …, sn – string or several strings;
  • res_str – the resulting string.

Example.

# Function min() - the use for the strings

# Case 1. Applied to one string
s = 'abc+,_+'
minimum = min(s) # min = '+'

minimum = min('2450') # min = '0'

# Case 2. Applied to multiple strings.
minimum = min('abc', 'def', 'jklmn') # min = 'abc' - considered string
minimum = min('+', '-', '*', '/') # min = '*'
minimum = min('Ivanov', 'Petrov', 'Sidorov') # min = 'Ivanov'

 

3. Function max(). Defining of item with the maximum code in the string

The built-in max() function can be applied to objects of various types, including strings.

The max() function can have two forms of call:

  • call of one string. In this case, the string is considered as a sequence of characters. The result of the function is the character that has the largest code in the character table;
  • a call with two or more string parameters. In this case, each string is considered separately. Strings form a sequence. The result of returning from the function is a string that follows the last in lexicographic order.

According to the Python documentation, the general form of using a function for strings is as follows:

res_str = max(s)

or

res_str = max(s1[, s2[, ... sn]])

here

  • s, s1, s2, …, sn – string or several strings;
  • res_str – the resulting string.

Example.

# Function max() - the use for the strings

# Case 1. Is used for a single string.
s = '+-ajwF 28'
maximum = max(s) # max = 'w'

maximum = max('2450') # max = '5'

# Case 2. Is use for several strings
maximum = max('abc', 'def', 'jklmn') # max = 'jklmn'
maximum = max('+', '-', '*', '/') # max = '/'
maximum = max('Ivanov', 'Petrov', 'Sidorov') # max = 'Sidorov'

 


Related topics