Python. Functions that process case-sensitive characters in a string




Functions that process case-sensitive characters in a string


Contents


Search other websites:

1. Function str.capitalize(). The first character of the string is capitalized

The capitalize() function returns a copy of the string with the first character in uppercase, and the other characters in lowercase.

Example.

# Function str.capitalize()
# Only letters are accepted
s1 = 'abcdef ghi'
s2 = str.capitalize(s1) # s2 = 'Abcdef ghi'

s1 = 't'
s2 = str.capitalize(s1) # s2 = 'T'

s1 = '234'
s2 = str.capitalize(s1) # s2 = '234'

s1 = 'ABCDEF GHI'
s2 = str.capitalize(s1) # s2 = 'Abcdef ghi'

# Accounting for the Cyrillic alphabet
s1 = 'байт!'
s2 = str.capitalize(s1) # s2 = '\xc1\xe0\xe9\xf2!'

 

2. Function str.casefold(). Return the folded copy of the string

The str.casefold() function returns a minimized copy of the string. The concept of a “casefolded copy” of a string means that in such a copy all the differences in the case of characters in the string are removed. The peculiarity of a “casefolded” copy of a string is that the lower() function cannot be applied to some characters, and the casefold() function can. An example of such a character is the German character ‘ß’, which in the casefold() function is replaced with ss characters, unlike the lower() function.

The function has been introduced in Python since version 3.3.

Example.

# Function casefold() - returns a folded copy of the string

# Use with strings containing uppercase and lowercase letters
s1 = 'Abcdef Ghi'
s2 = str.casefold(s1) # s2 = 'abcdef ghi'
s3 = s1.casefold() # s3 = 'abcdef ghi'

# use with uppercase strings
s4 = 'ABCDEF SS'
s5 = s4.casefold() # s5 = 'abcdef ss'

 

3. Function str.lower(). Character case conversion

The str.lower() function converts characters to lowercase.

Example.

# Function str.lower()

s1 = 'ABCD'
s2 = s1.lower() # s2 = 'abcd'

s2 = 'Hello World!'.lower() # s2 = 'hello world!'


 

4. Function str.swapcase(). Return a copy of a string with transformation of lowercase to uppercase characters

The str.swapcase() function returns a copy of a string with uppercase letters converted to lowercase and, conversely, lowercase converted to uppercase. The general form of a function call is as follows:

s2 = s1.swapcase()

where

  • s1 – the specified string to be converted
  • s2 – the resulting string in which all uppercase characters are converted to lowercase, and all lowercase characters are converted to uppercase.

For this function, it cannot be stated that the following expression

s.swapcase().swapcase() == s

will always be executed.

Example.

# Function str.swapcase()

s1 = 'Abcd'
s2 = s1.swapcase() # s2 = 'aBCD'

s1 = 'a+5+BC+D'
s2 = str.swapcase(s1) # s2 = 'A+5+bc+d'

s2 = 'hELLO WORLD'.swapcase() # s2 = 'Hello world'

# For cyrillic
s1 = 'wEB-SITE'
s2 = s1.swapcase() # s2 = 'Web-site'

 

5. Function str.title(). Return the string with uppercase letters in words

The str.title() function returns a string in which all words begin with a capital letter. Other characters in these words are lowercase. The general form of using the function is as follows:

s2 = s1.title()

where

  • s1 – source string;
  • s2 – the resulting string in which the correction of characters that are the beginning of words is implemented.

The function has one feature. The character ‘\” of the apostrophe forms the boundary of the word. In some cases this is undesirable. To avoid this drawback, you need to use regular expressions.

Example.

# Function str.title

s1 = 'hello world!' # s2 = 'Hello World!'
s2 = s1.title()

s1 = 'HELLO WORLD!'
s2 = str.title(s1) # s2 = 'Hello World!'

# A separate case with the apostrophe symbol '\''
s1 = "I'm happy!"
s2 = s1.swapcase() # s2 = 'i'M HAPPY!'

 

6. Function str.upper(). Convert string characters to uppercase

The str.upper() function allows you to get a copy of the string in which all characters are in upper case. According to the Python documentation, the general form of the function is as follows:

s2 = s1.upper()

where

  • s1 – source string;
  • s2 – the resulting string is a copy in which the lowercase characters of the string s1 are replaced with uppercase characters.

Character conversion is performed only for uppercase characters. These are characters that are included in the following categories:

  • “Lu” – Letter uppercase;
  • “Ll” – Letter lowercase;
  • “Lt” – Letter titlecase.

Example.

# Function str.upper()

s1 = 'abcdef'
s2 = s1.upper() # s2 = 'ABCDEF'

s1 = '' # empty string
s2 = str.upper(s1) # s2 = ''

s1 = '5+6=11'
s2 = s1.upper() # s2 = '5+6=11'

s2 = 'aBc deFg'.upper() # s2 = 'ABC DEFG'

 


Related topics

 


 

0
fb-share-icon20