String Enters the Arena

The next Superpower of Python which we are going to study is “Strings”- a text based information which is a collection of ordered collection of characters. In this Blog we are going to discuss on how to store text in variables, concatenation,use text as expressions and many more. Lets begin our tour.

Strings

Strings are little chunks of text which can be used to store it in a variable, display it and has many other purposes.We have used strings in many languages but Python’s strings has some special properties like there is no distinct type of characters in strings, instead we just use one character strings. Another main property of Strings in Python is their immutable property, which means characters are placed in ordered fashion from left to right.

Working with strings is sort of easy, we face difficulty when we have to write them in code, because there are many ways in which a string can be written.

  1. Single Quotes: ‘Python”s’
  2. Double Quotes: ”Python’s”
  3. Escape Sequences: “P\ty\ntho\ons”

and there others also like Triple quotes,Byte strings, Unicode Strings and Raw strings, but at beginner level it will be enough to know the first 3 types mentioned in points.

→ The quotes are there only to tell the computer where the string begins and ends

(and is not part of the string value).

→ There’s not much difference in Single quotes and Double Quotes usage, both are same actually. The Reason for supporting both is that it will allow one to write a quote character of other type without the usage of backslash. It will be clear from the code given:

Code:

>>> string=”Monty Python's”
>>> string
“Monty Python's”
>>> string='Monty Python”s'
>>> string
'Monty Python”s'
>>> string="hello"s"
SyntaxError: invalid syntax
>>> string='python's'
SyntaxError: invalid syntax

String Concatenation

We can combine one string with other string, which is called String concatenation. We can do this using ‘+’ operator or by using Space Character.

>>> var="Guido " + "van" + " Rossum"
>>> var
'Guido van Rossum'
>>> var="Guido " "van" " Rossum"
>>> var
'Guido van Rossum'

→When using comma instead of the concatenation operator between the strings, we will get Tuple not a string.

>>> var = "string1,string2"
>>> var
'string1,string2'

Note: Strings and Integers are different Datatype.

>>> var= "hello "+ 5
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
var= "hello "+ 5
TypeError: cannot concatenate 'str' and 'int' objects

This error came because we tried to concatenate String and Integer Objects.

Strings in Action

Since we have gained some knowledge of string literals, lets work on this. This part of blog will demonstrate String expressions , methods and formatting.

We have seen how to concatenate two strings, now lets see how strings can be repeated using * operator.

>>> var= 'ha' * 6
>>> print var
hahahahahaha #repetition of 'ha' 6 times

similarly, we can find the length of the strings or other objects which has a length.

>>> len(var)
12
>>> len(var * 6)
72
>>> len ('abc'+"def")
6

Repetition comes handy in many applications, consider a case where you want to enter ‘ – ‘ 120 times for the end of a diary.

>>> endline='-' * 120
>>> print endline
------------------------------------------------------------------------------------------------

Its better than to write 120 dashes.

String Conversion tool

In some situation, lets take concatenation as an example, when u want to concatenate string and integer we get an error because both the data type is different. We can overcome from this problem by conversion.

>>> 'abc'+123
Traceback (most recent call last):
File "<pyshell#37>", line 1, in <module>
'abc'+123
TypeError: cannot concatenate 'str' and 'int' objects
>>>'abc'+str(123)
'abc123'
>>> int('123')+321
444
>>> repr(24)
'24'

The str function converts a integer into a string, while int function converts a string into an integer and repr funtions similar to str that it converts an object into string.

>>> str(123),repr(123)
('123', '123')
>>> str('abc'),repr('abc')
('abc', "'abc'")

In my next blog, we will be discussing on slicing and indexing which acts as an array function in python and then will move onto how we can change the strings, joining a string and finally will discuss on string as expression.

Advertisement