CodingBison

Strings allow us to store and process text-based data. A Python string is an immutable sequence of characters; these characters can be alphabets, numbers (from 0 to 9), white-spaces, etc. We can access (but not modify!) individual characters using the offset.

Like other sequences (tuples and dictionaries), we can use the len() function to find the number of characters present in the string sequence as well.

The following figure shows a Python string variable (varStr) that holds the text: "Polar Bear".



Figure: A Python string

Let us start with a simple example. It defines a simple string variable; the type() function returns "str" for Python strings. It also accesses an individual character using its offset.

 [user@codingbison]$ python3
 Python 3.2.1 (default, Jul 11 2011, 18:55:33) 
 [GCC 4.6.1 20110627 (Red Hat 4.6.1-1)] on linux2
 Type "help", "copyright", "credits" or "license" for more information.
 >>> 
 >>> varStr = "Polar Bear"
 >>> 
 >>> print(type(varStr))
 <class 'str'>
 >>> 
 >>> print(varStr) 
 Polar Bear
 >>> print(varStr[6])
 B
 >>> print(len(varStr))
 10
 >>> 

Since strings are a sequence of characters, we can run a loop over the sequence.

 >>> varStr = "Bear"
 >>> 
 >>> for x in varStr:
 ...     print(x)
 ... 
 B
 e
 a
 r
 >>> 

Besides double quotes, we can also use single or triple quotes for defining strings.

 >>> varStr = 'Polar Bear'
 >>> print(varStr)
 Polar Bear
 >>> 
 >>> varStr = '''Polar Bear'''
 >>> print(varStr)
 Polar Bear
 >>> 

Python strings are immutable. This means that once a string object is created, it cannot be modified. If we try to change a character in the string, then Python indicates its unhappiness that string, being immutable, does not support assignment.

 >>> varStr = "Polar Bear"
 >>> 
 >>> varStr[6] = 'D'
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
 TypeError: 'str' object does not support item assignment
 >>> 

However, it is completely possible to assign a new string object to an existing string variable! When we assign a new string to an existing string variable, then Python does not modify the earlier string object. Instead, it simply creates a new string object and makes the string variable point to the new object. The original string object remains as is!

String Concatenation

We can concatenate string using the "+" operator. The resulting object is also of string type. Let us see an example.

 >>> varStrName = "Polar Bears"
 >>> print(len(varStrName))
 11
 >>> varStrAction = " can swim for long distances"
 >>> print(len(varStrAction))
 28
 >>> varStrNew = varStrName + varStrAction
 >>> print(len(varStrNew))
 39
 >>> print(varStrNew)
 Polar Bears can swim for long distances
 >>> 

However, Python throws a TypeError if one of the values being concatenated is not a string object. To avoid this error, we can use the str() method to cast non-string objects into string objects.

 >>> varStr1 = "A Polar bear with "
 >>> varNum1 = 2
 >>> varStr2 = " cubs"
 >>> 
 >>> varStrNew = varStr1 + varNum1 + varStr2
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
 TypeError: Can't convert 'int' object to str implicitly
 >>> 
 >>> varStrNew = varStr1 + str(varNum1) + varStr2
 >>> print(varStrNew)
 A Polar bear with 2 cubs
 >>> 

We can also create repetitions of a given string using the "*" operator. With this operator, we create multiples of a given string and concatenate them together. Note that this is an overloading of the "*" operator. For numbers, this operator is used for arithmetic multiplication.

 >>> varStr = "Polar Bear"
 >>> 
 >>> varStrNew = varStr * 3
 >>> print(varStrNew)
 Polar BearPolar BearPolar Bear
 >>> 

Another approach for concatenation is to use join() method. We will discuss this method in the section on String methods.

String Formatting

Python strings support C like formatting. There are several styles to do that: (a) (string % tuple) substitution, (b) (string % dictionary) substitution, (c) pass a string or a number.

For (string % tuple) substitution, a formatted string can input values from the provided tuple. In the following example, the first object is a string where values are substituted from the tuple provided after the "%" operator.

 >>> varTuple =  ("Ursus maritimus", "Alopex lagopus")
 >>> 
 >>> print("Polar Bear: %s \t Arctic Fox: %s" % varTuple)
 Polar Bear: Ursus maritimus      Arctic Fox: Alopex lagopus
 >>> 
 >>> print("Polar Bear: %s \t Arctic Fox: %s" % ("Ursus maritimus", "Alopex lagopus"))
 Polar Bear: Ursus maritimus      Arctic Fox: Alopex lagopus
 >>> 

For (string % dictionary) substitution, a formatted string can input values from the provided dictionary object. Don't forget to add "s" after the dictionary keys!

 >>> scientificNames = {"Polar Bear": "Ursus maritimus", "Arctic Fox": "Alopex lagopus"}
 >>> 
 >>> print("Polar Bear: %(Polar Bear)s \t Arctic Fox: %(Arctic Fox)s" % scientificNames)
 Polar Bear: Ursus maritimus      Arctic Fox: Alopex lagopus
 >>> 

Lastly, we can also pass strings and numbers for the substitution.

 >>> print("Polar Bear: %s " % "Ursus maritimus")
 Polar Bear: Ursus maritimus 
 >>> 
 >>> print("A mother Polar bear with %s cubs" % 2) 
 A mother Polar bear with 2 cubs
 >>> 




comments powered by Disqus