More About Strings

Python has several ways to access individual characters in a string, and has methods that let you perform operations on them.

String Operations

Accessing Individual Characters in a String

One of the easiest ways to access individual characters within a string is to use the for loop.

for variable in string:
    statement
    statement
    etc.

You can also access individual characters in a string with an index. Each string character is treated like an element in a list starting with position "0".

String Concatenation

You can append one string to another using the "+" operation.

String are immutable - once created they CANNOT be changed.

String Slicing

Slicing expressions can be used to select a range of characters from a string.

string[start : end]

Testing, Searching and Manipulating Strings

There are operators and methods for testing strings, searching the contents of strings, and getting modified copies of strings.

in And not in Operators

string 1 in string 2

String Methods

stringvar.method(arguments)

Method Explanation

isalnum()

returns true if string contains only alphabetic letters or digits and is at least 1 character in length - returns false otherwise

isalpha()

returns true if the string contains only alphabetic letters and is at least 1 character in length - returns false otherwise

isdigit()

returns true if the string contains only numeric digits and is at least 1 character in length - returns false otherwise

islower()

returns true if all the alphabetic letters are lowercase, and contains at least 1 alphabetic characters - returns false otherwise

isspace()

returns true if the string contains only whitespace characters and is at least 1 character in length - returns false otherwise
(whitespace characters are: spaces, newlines (\n), and tabs (\t))

isupper()

returns true if all of the alphabetic letters are uppercase, and the string has at least 1 alphabetic letter - returns false otherwise

Modification Methods

Method Explanation

lower()

returns copy with all characters converted to lowercase

lstrip()

returns copy with all whitespace characters removed

lstrip(char)

char argument is a string containing a characters - returns copy of the string with all instances of char that appear at the beginning of the string removed

rstrip()

returns copy with all whitespace characters at the end removed

rstrip(char)

same as lstrip(char) but at the end

strip()

returns a copy of string with all leading and trailing whitespace characters removed

strip(char)

returns a copy of string with char characters at beginning and end removed

upper()

returns a coy of the string with all alphabetic letters converted to uppercase.

Search and Replace Methods

Method Explanation

endswith(substring)

substring argument is a string - returns true if the string ends with substring

find(substring)

substring argument is a string - returns lowest index in the string where substring is found

replace(old, new)

old and new arguments are both strings - returns a copy of the string with all instances of old replaced by new

startswith(substring)

substring is a string - returns true if the string starts with the substring

Repetition Operator

string_to_copy * n

Exercises

info

Note the >>> prompt in the examples below indicates that they are running in the PYTHON shell using the "interactive" mode.

The same method for indexing for lists can be applied to strings as well.

Keep in mind strings are immutable, which means they CANNOT be modified.

>>> book = "Alchemist"
>>> book[0]
'A'
>>> book[3]
'h'
>>> book[-1]
't'
>>> book[10]
Traceback (most recent call last):
  File "", line 1, in
IndexError: string index out of range

>>> book[2:6]
'chem'
>>> book[:5]
'Alche'
>>> book[5:]
'mist'
>>> book[::-1]
'tsimehclA'
>>> book[:]
'Alchemist'

>>> list(book)
['A', 'l', 'c', 'h', 'e', 'm', 'i', 's', 't']

>>> import string
>>> string.ascii_lowercase[:10]
'abcdefghij'
>>> list(string.digits)
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

Looping over strings.

>>> book
'Alchemist'

>>> for char in book:
        print(char)
    
A
l
c
h
e
m
i
s
t

Striping characters from a string.

  • removing leading/trailing/both characters
  • only consecutive characters from start/end string are removed
  • by default whitespace characters are stripped
  • if more than one character is specified, it is treated as a set and all combinations of it are used

>>> greeting = ' Have a nice day :) '
>>> greeting.strip()
'Have a nice day :)'
>>> greeting.rstrip()
' Have a nice day :)'
>>> greeting.lstrip()
'Have a nice day :) '

>>> greeting.strip(') :')
'Have a nice day'

>>> greeting = '===== Have a great day!! ====='
>>> greeting.strip('=')
' Have a great day!! '

Changing case and case checking.

>>> sentence = 'thIs iS a saMple StrIng'

>>> sentence.capitalize()
'This is a sample string'

>>> sentence.title()
'This Is A Sample String'

>>> sentence.lower()
'this is a sample string'

>>> sentence.upper()
'THIS IS A SAMPLE STRING'

>>> sentence.swapcase()
'THiS Is A SAmPLE sTRiNG'

>>> 'good'.islower()
True

>>> 'good'.isupper()
False

Check if string is made up of numbers.

>>> '1'.isnumeric()
True
>>> 'abc1'.isnumeric()
False
>>> '1.2'.isnumeric()
False

Check if character sequence is present or not.

>>> sentence = 'This is a sample string'
>>> 'is' in sentence
True
>>> 'this' in sentence
False
>>> 'This' in sentence
True
>>> 'this' in sentence.lower()
True
>>> 'is a' in sentence
True
>>> 'test' not in sentence
True

Get number of times character sequence is present (non-overlapping).

>>> sentence = 'This is a sample string'
>>> sentence.count('is')
2
>>> sentence.count('w')
0

>>> word = 'phototonic'
>>> word.count('oto')
1

Matching character sequence at start/end of string.

>>> sentence
'This is a sample string'

>>> sentence.startswith('This')
True
>>> sentence.startswith('The')
False

>>> sentence.endswith('ing')
True
>>> sentence.endswith('ly')
False

Splitting strings based on character sequence.

  • returns a list

>>> sentence = 'This is a sample string'

>>> sentence.split()
['This', 'is', 'a', 'sample', 'string']

>>> "oranges:5".split(':')
['oranges', '5']
>>> "oranges :: 5".split(' :: ')
['oranges', '5']

>>> "a e i o u".split(' ', maxsplit=1)
['a', 'e i o u']
>>> "a e i o u".split(' ', maxsplit=2)
['a', 'e', 'i o u']

Replacing characters in a string.

  • third argument specifies how many times replace has to be performed
  • since strings are immutable, variable has to be explicitly re-assigned to change its value

>>> phrase = '2 be or not 2 be'
>>> phrase.replace('2', 'to')
'to be or not to be'

>>> phrase
'2 be or not 2 be'

>>> phrase.replace('2', 'to', 1)
'to be or not 2 be'

>>> phrase = phrase.replace('2', 'to')
>>> phrase
'to be or not to be'

Further Reading