< previous | Table of Contents | next > |
6.3 STRING METHODS
String methods are built-in functions that enable you to more easily work with String objects - to manipulate them, to search with them, or to change their appearance.
CHANGING CASES
For dealing with case-sensitive operations, such as when trying to check if a substring exists within a given string using the in
operator, we need to be able to alter the case of a string. To do so, Python provides us with helpful built-in functions.
str.upper()
converts all characters of a given string to uppercasestr.lower()
converts all characters of a given string to lowercasestr.title()
converts the first character of each word to uppercase and the rest to lowercase.str.capitalize()
converts the first character to uppercase and the rest to lowercase
Here you can see an example in Thonny of what all these methods would output if used on the same string.
DETERMINING THE LENGTH OF A STRING
The len()
function is a built-in function that returns the number of items in an object. We can use it on strings as well as other data types, such as lists. For strings, len()
returns the number of characters in a given string.
Here’s a simple example in Thonny to illustrate. Notice that the spaces between words are also counted.
SPLITTING AND JOINING
Splitting and joining strings allows you to divide strings into a list of substrings or concatenate a multiple of strings into a single string. To do so, we can use the split()
method, which divides a string into a list of substrings based on a specified separator (also known as a delimiter), such as whitespace, commas, semicolons, etc. If we don’t specify how to split the string, it will be split according to whitespace within the string by default.
Below we see that how we can both split string by whitespace or by another delimiter, such as a comma. Note that the splitting operation returns a list of elements.
Joining strings is the process of concatenating the elements of an iterable, such as a list, into a single string. We can use the str.join()
method to do so. The str
before the join is, in this case, known as a separator. This specifies how you want the strings to be combined, whether you want to enter spaces between them or something else.
Here’s an example in Thonny of how we can join items of a list into a string with a desired separator between the words.
TRIMMING
Trimming strings in Python involves removing unwanted characters, such as whitespace, from the beginning and end of a string. If we don't pass any arguments to the function, then it defaults to removing the whitespace only. This comes in handy when we parse data that comes in an unknown format or structure, for example, so we can make it consistent for correct parsing.
str.strip()
removes the leading and trailing characters from a string.str.lstrip()
removes the leading characters from the start of the stringstr.rstrip()
removes the trailing characters from the end of the string.
test_string = " Quidditch is a fun game. " print(test_string.strip()) # prints "Quidditch is a fun game." print(test_string.lstrip()) # prints "Quidditch is a fun game. " print(test_string.rstrip()) # prints " Quidditch is a fun game."
ISNUMERIC() AND ISDIGIT()
We can use methods such as isnumeric()
, isdigit()
, and more, to check the properties of a string. These boolean methods help you determine whether a String has certain properties.
In the context of this course, you should understand the isnumeric()
method, which helps check whether all the characters in a presented string are numeric. Integers and floating point numbers are numeric characters. It has a broader definition of numeric characters than isdigit()
, including fractions, Roman numerals, etc.
test_numeric1 = “123” test_numeric2 = “1.5” print(test_numeric1.isnumeric()) # Outputs True print(test_numeric2.isnumeric()) # Outputs True
The isdigit()
is used to check whether all the characters provided in a string are digits, meaning characters from 0 through to 9.
test_digit1 = “123” test_digit2 = “1.5” print(test_digit1.isdigit()) # Outputs True print(test_digit2.isdigit()) # Outputs False as a fraction is not considered a digit.
KEY THINGS TO REMEMBER
str.upper()
,str.lower()
,str.title()
,str.capitalize()
allow us to change the cases of strings.len()
returns the number of characters in a string.str.split()
divides a string into a list of substrings based on a specified delimiter.str.join()
concantenates elements of an iterable into a single string, separated by a specified string.str.strip()
,str.lstrip()
,str.rstrip()
allow us to trim leading and trailing characters of strings.str.isdigit()
checks if all characters in the string are digits (numbers from 0-9).str.isnumeric()
checks if all characters are numeric, including digits, fractions and more.
SELF-CONTROL EXERCISES
< previous | Table of Contents | next > |