Python String Methods

Continuing our last post, Python Strings, we will explore Python string methods.  Python strings have built-in methods that extend their use. It is good to note that it will not change the string when you run the method against the string, as they are immutable. If you want to modify the string, assign the method call to a new variable.

Looking at a few common Python string methods using the following: mystr = 'aBcdefGhI'

A few string methods

  • .capitalize()
    • This will return the string with the first letter capitalized.
      mystr.capitalize()
      'Abcdefghi'
  • .lower()
    • This will return the string with all lowercase letters.
      mystr.lower()
      'abcdefghi'
  • .upper()
    • This will return the string with all upper-case letters.
      mystr.upper()
      'ABCDEFGHI'
  • .split()
    • The string will split at every instance of the supplied delimiter.
    • Returned in a list.
    • For this one, we are going to change our variable.
    • By default, if you do not pass anything into the method, it will split on every instance of a white space.
      my_new_str = "Newbridge Circle is my street"
      my_new_str.split()
      ['Newbridge', 'Circle', 'is', 'my', 'street'
  • .isalnum()
    • This will return a boolean if the string is alphanumeric
      mystr.isalnum()
      True
  • isalpha()
    • This will return a boolean if the string only has letters of the alphabet in it.
      my_new_str.isalpha()
      False
    • If there is white space in the string, it will return False

String Concatenation

You can also concatenate strings together using the + operator. When doing this, you need to make sure that you are using the operator on two of the same data type, meaning string + string or int + float. Since strings are immutable, this is useful if you want to combine strings.  If you want to print the blog\\\’s name, but the name is in two different variables, you can concatenate them together.

n1 = 'Routed'
n2 = 'Interface'
n1+n2
'RoutedInterface'

It did not add a space between the two variables because there is no white space. All string concatenation does is take the two strings and puts them together. If we wanted space between the two variables, we would add a trailing space in n1 or start with a white space in n2. We can also add white space by doing the following:

n1+' '+n2
'Routed Interface'

We can use white space in our string concatenation since white space is a string.

To show what happens when you try to concatenate with unlike data types, say a string and an integer:

phrase = 'Hello Everyone! My age is: '
age = 38

When we try to concatenate phrase and age:

phrase+age
Traceback (most recent call last): File "", line 1, in
TypeError: can only concatenate str (not "int") to str

You can check the type of an object if you are unsure of the type() method.

type(phrase)
<class str'>
type(age)
<class 'int'>

To be able to concatenate these two objects, we need to turn age into a str

phrase = 'Hello Everyone! My age is: '
age = '38'
phrase + age
'Hello Everyone! My age is: 38'

Another thing we can do is use the multiplication * operator to repeat an object. To add some formatting when we are printing this, we can print a dash (-) 15 times:

'-' * 15
'---------------'

The official documentation for Python string methods can be found at https://docs.python.org/3.8/library/stdtypes.html#string-methods.