Home > Articles > Programming > Python

  • Print
  • + Share This
This chapter is from the book

Getting Information About a String

In Hour 2, “Putting Numbers to Work in Python,” variables were compared to cups because they can hold a number of things. Cups themselves have some basic functions, too, whether they contain something or not. You can move them around, you can touch their side to see if what’s in them is hot or cold, and you can even look inside them to see if there’s anything in there. The same goes with strings.

Python comes with a number of built-ins that are useful for getting information about the stored text and changing how it’s formatted. For example, we can use len() to see how long a string is.

In the following example, we want to see how long a name is:

>>> name = "katie"
>>> len(name)
5

In this case, the length of the string held in name is five.

In Python, variables also come with some extra capabilities that allow us to find out some basic information about what they happen to be storing. We call these methods. Methods are tacked on to the end of a variable name and are followed by parentheses. The parentheses hold any information the method might need. Many times, we leave the parentheses blank because the method already has all the information it requires.

One set of methods that comes with strings is used to change how the letters are formatted. Strings can be converted to all caps, all lowercase, initial capped (where the first letter of the string is capitalized), or title case (where the first letter and every letter after a space is capitalized). These methods are detailed in Table 4.1.

TABLE 4.1 String-Formatting Methods

Method

Description

Example

.upper()

Converts all letters to uppercase (a.k.a. all caps).

'HELLO WORLD'

.lower()

Converts all letters to lowercase.

'hello world'

.capitalize()

Converts the first letter in a string to uppercase and converts the rest of the letters to lowercase.

'Hello world'

.title()

Converts the first letter, and every letter after a space or punctuation, to uppercase. The other letters are converted to lowercase.

'Hello World'

These methods are appended to the end of a string (or variable containing a string):

>>> title = "wind in the willows"
>>> title.upper()
'WIND IN THE WILLOWS'
>>> title.lower()
'wind in the willows'
>>> title.capitalize()
'Wind in the willows'
>>> title.title()
'Wind In The Willows'

These methods are nondestructive. They don’t change what’s stored in the variable. In the following example, note that the string stored in movie_title isn’t changed, even though we used .upper() on it:

>>> movie_title = "the mousetrap"
>>> movie_title.upper()
'THE MOUSETRAP'
>>> movie_title '
the mousetrap'

We can also see if certain things are true about a string. is_alpha() and is_digit() are two popular methods, especially when checking to see if a user put in the correct type of data for a string.

In the following string, we check to see that birth_year is composed of all digits and that state is nothing but letters:

>>> birth_year = "1980"
>>> state = "VA"
>>> birth_year.isdigit()
True
>>> state.isalpha()
True

Had birth_year contained any letters or symbols (or even spaces), isdigit() would have returned False. With state, had it contained any numbers or symbols, we would have gotten False as well.

>>> state = "VA"
>>> state.isdigit()
False
  • + Share This
  • 🔖 Save To Your Account