Python. Class str. Functions for working with strings that determine the features of a string

Class str. Functions for working with strings that determine the features of a string


Contents


1. Functions for working with strings that determine the characteristics of a string. The list

The following functions of the str class are introduced in the Python library to determine features of a string:

  • isalnum();
  • isalpha();
  • isascii();
  • isdecimal();
  • isdigit();
  • isidentifier();
  • islower();
  • isnumeric();
  • isprintable();
  • isspace();
  • istitle();
  • isupper().

 

2. Function. str.isalnum(). Check alphanumeric characters in  the string

The str.isalnum() function returns True if both conditions are true:

  • all characters in the string are alphanumeric;
  • string contains at least one character.

Otherwise, the function returns False.

A sym character is considered alphanumeric if one of the following functions returns True:

  • sym.isalpha();
  • sym.isdecimal();
  • sym.isdigit();
  • sym.isnumeric().

Example.

# Function str.isalnum()

s = '' # empty string
f = s.isalnum() # f = False

s = 'abcd'
f = s.isalnum() # f = True

s = '123'
f = str.isalnum(s) # f = True

s = 'A1'
f = s.isalnum() # f = True

f = '+-*'.isalnum() # f = False

f = 'a b'.isalnum() # f = False

 

3. Function str.isalpha(). The check of alphabetic characters in a string

The str.isalpha() function returns True if both conditions are true:

  • all characters of the string are alphabetic;
  • there is at least one character in the string.

Otherwise, the function returns False.

According to the Unicode standard, alphabetic characters correspond to the properties “Lm”, “Lt”, “Lu”, “Ll”, “Lo”.

Example.

# Function str.isalpha()

s = '' # empty string
f = s.isalpha() # f = False

s = 'abcd'
f = s.isalpha() # f = True

s = '123'
f = str.isalpha(s) # f = False

s = 'A1'
f = s.isalpha() # f = False

f = '+-*'.isalpha() # f = False

f = 'a b'.isalpha() # f = False

 

4. Function str.isascii(). Check string for ASCII characters

The str.isascii() function returns True when one of the conditions is true:

  • all characters in the string are ASCII-characters;
  • the string is empty.

ASCII characters are characters whose codes are in the range 00-7F in hexadecimal notation.

Example.

# Function str.isascii - returns True if there are ASCII characters in the string

# Applying a function to a non-empty string

s = ";-ab9" # All characters in the range from 0 to 7F
f = str.isascii(s) # f = True

# There are two characters 'Ю' and 'й' which are outside the range of 0..7F
s = "Юй09,"
f = s.isascii() # f = False

# Applying a function to an empty string
f = ''.isascii() # f = True

 

5. Function str.isdecimal(). Determining if a string contains decimal characters

The str.isdecimal() function returns True if all characters in the string are decimal. If there is at least one non-decimal character in the string, then the function returns False.
The decimal symbol is the symbol used to form numbers at the base of 10. In other words, decimal characters are the digits ‘0’, ‘1’, ‘2’, …, ‘9’.
In the Unicode decimal symbols belong to the category “Nd”.

Example.

# Function str.isdecimal() - determines whether all characters in a string are decimal

# Case 1. A non-empty string
s = '123'
f = s.isdecimal() # f = True

f = 'A123'.isdecimal() # f = False, the string contains the character 'A'

s = '5.8'
f = s.isdecimal() # f = False, the string contains the character '.'

# Case 2. Empty string
s = ''
f = str.isdecimal(s) # f = False

 

6. Function str.isdigit(). Determining whether all characters are digits in a string

The str.isdigit() function returns True if both of the following conditions are true:

  • all characters in a string are numbers;
  • the string contains at least one character.

Otherwise, the function returns False.

Digits are symbols:

  • decimal characters ‘0’..‘9’;
  • digits requiring special processing. Such numbers cannot be used to form numbers at base 10. For example, superscript compatibility index digits.

In Python, digits are considered characters with property values Numeric_Type = Digit or Numeric_Type = Decimal.

Example.

# Function str.isdecimal

# Case 1. Non-empty string
s = '123+'
f = s.isdigit() # f = False

s = '-18'
f = s.isdigit() # f = False

s = '' # empty string
f = str.isdigit(s) # f = False

s = '2.8'
f = str.isdigit(s) # f = False

s = '3209'
f = s.isdigit() # f = True

 

7. Function str.isidentifier(). Determining if a string is an identifier or keyword

The str.isidentifier() function returns True if the string is an identifier or a keyword that is valid according to the Python syntax.

Example.

# Function str.isidentifier()

s = "Hello"
f = s.isidentifier() # f = True

s = "a1"
f = str.isidentifier(s) # f = True

s = "1a"
f = s.isidentifier() # f = False - not identifier and not keyword

s = 'for'
f = s.isidentifier() # f = True - keyword

f = 'class'.isidentifier() # f = True - keyword

 

8. Function str.islower(). Does the string contain lowercase letters

The str.islower() function returns True if both conditions are true:

  • all characters of a string (except for digital ones) are lowercase;
  • string contains at least one character.

Otherwise, the function returns False.

Example.

# Function str.islower()

s = "hello"
f = s.islower() # f = True

s = 'Hello'
f = str.islower(s) # f = False

s = '123' # all are digits
f = s.islower() # f = False

s = 'a123' # at least one lowercase letter
f = s.islower() # f = True

s = 'A123' # one letter in uppercase
f = s.islower() # f = False

f = '-+;:'.islower() # f = False

 

9. Function str.isnumeric(). Does the string contain numeric characters?

The str.isnumeric() function returns True if both of the following conditions are true:

  • all characters of the string are numeric;
  • at least one character exists in the string.

Numeric characters are characters that include:

  • digits ‘0’, ‘1’, …, ‘9’;
  • all characters that have the Unicode numeric property.

In Python, for numeric characters, the Numeric_Type property is one of the values:

  • Digit;
  • Decimal;
  • Numeric.

Example.

# Function str.isnumeric()

# Case 1. Empty string
s = "" # f = False

# Case 2. Non-empty string
s = 'abcd'
f = s.isnumeric() # f = False

s = '1234'
f = s.isnumeric() # f = True

s = 'A1'
f = str.isnumeric(s) # f = False

s = '2.5'
f = s.isnumeric() # f = False

f = '-8'.isnumeric() # f = False

 

10. Function str.isprintable(). Are there any characters in the string suitable for printing?

The str.isprintable() function returns True if one of the conditions is true:

  • all characters in the string are printable;
  • the string is empty.

Otherwise, the function returns False.

Printable characters are defined in the Unicode character database as “Other” or “Separators,” except for the space character (ASCII code 0x20).

Example.

# Function str.isprintable()

s = '' # empty string
f = s.isprintable() # f = True

s = 'abcd ef12'
f = s.isprintable() # f = True

s = '+-;:,.'
f = str.isprintable(s) # f = True

s = '\n\tabcdef'
f = s.isprintable() # f = False, the string contains '\n', '\t'

 

11. Function str.isspace(). Whitespace check

The str.isspace() function returns True if both of the following conditions are true:

  • there are only whitespace characters in the string;
  • there is at least one character in the string.

Otherwise, the function returns False.

Whitespace characters are characters:

  • which are defined in the Unicode database as “Other” or “Separator”;
  • which have a bidirectional property of type “WS”, “B” or “S”.

Example.

# Function str.isspace()
s = '' # empty string
f = s.isspace() # f = False

s = ' ' # space character
f = str.isspace(s) # f = True

s = 'a + b'
f = s.isspace() # f = False

s = '\t' # tab
f = s.isspace() # f = True

s = '\n' # newline character
f = str.isspace(s) # f = True

s = '\n\t'
f = s.isspace() # f = True

 

12. Function str.istitle(). Title check

The str.istitle() function returns True if both conditions are true:

  • the string has a title;
  • there is at least one character in the string.

Otherwise, the function returns False.

Example.

# Function str.istitle()

s = 'Title string'
f = s.istitle() # f = False

s = 'T'
f = s.istitle() # f = True

s = ''
f = str.istitle(s) # f = False

s = 'aA'
f = s.istitle() # f = False

s = '+5'
f = s.istitle() # f = False

 

13. Function str.isupper(). Does the string contain uppercase characters?

The str.isupper() function returns True if both conditions are true:

  • all characters in a string in uppercase;
  • there is at least one uppercase character.

Otherwise, the function returns False.

Example.

# Function str.isupper()

s = 'abcd'
f = s.isupper() # f = False

s = '' # empty string
f = s.isupper() # f = False

s = 'Abcd'
f = str.isupper(s) # f = False

s = 'ABCD'
f = s.isupper() # f = True

s = 'A123'
f = s.isupper() # f = True

s = '1Aa23'
f = s.isupper() # f = False

 


Related topics