Python: Strings data type

1. Variables are not associated with data types unlike C and C++.

2. Data types however are associated with objects.

3. Variables reference to objects. This is very similar to pointers in C.


cyrus@minty / $ python
Python 2.7.2+ (default, Oct  4 2011, 20:06:09) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> fruits = "apple"
>>> fruits
'apple'
>>> 

fruits is the variable and apple is the object. To refer the data apple I use fruits to point to the data.

To find the memory location of where data apple is stored I can use id() function.


>>> id(fruits)
140316369410256
>>> 

To convert to hexadecimal I can use the hex() function.


>>> hex(id(fruits))
'0x7f9df35ad8d0'
>>> 

An alternative is to use method wrapper to locate the memory location where the data apple is stored.


>>> fruits.__repr__
<method-wrapper '__repr__' of str object at 0x7f9df35ad8d0>
>>> 

Ways to define strings
I can use “…” double quotes.


>>> myname = "cyrus"
>>> myname
'cyrus'
>>> print myname
cyrus
>>> 

I can use ‘….’ single quotes.


>>> myname = 'cyrus'
>>> myname
'cyrus'
>>> print myname
cyrus
>>> 

I can insert a new line so that there is a new line.


>>> myname = "cyrus\nlok"
>>> myname
'cyrus\nlok'
>>> print myname
cyrus
lok
>>> 

If I do not want special characters to be interpreted I can use r prefix for my string.


>>> myname = r"cyrus\nlok"
>>> myname
'cyrus\\nlok'
>>> print myname
cyrus\nlok
>>> 
>>> myname = r'cyrus\nlok'
>>> myname
'cyrus\\nlok'
>>> print myname
cyrus\nlok
>>> 

As clearly seen from the examples above, if I want to directly knows what is stored in the memory I simply use the variable. If I want to print out the string data from the memory I use the command print

I can format the string if I want to.


>>> mystory = """
... My name is Cyrus, and this is my story.
... I started as a network engineer, and due to new opportunity I am now
... doing security as well 🙂
... """
>>> 
>>> mystory
'\nMy name is Cyrus, and this is my story.\nI started as a network engineer, and due to new opportunity I am now\ndoing security as well :)\n'
>>> 
>>> print mystory

My name is Cyrus, and this is my story.
I started as a network engineer, and due to new opportunity I am now
doing security as well 🙂

>>> 

As can be seen clearly, new line special characters were added automatically as I press enter.

Unicode
1. For internationalization, this is because in this world there are countless of languages, and to encode these we can use unicode.

2. The u prefix is prepended in front of the string data type.

>>> myunicode = u'xmen'
>>> myunicode
u'xmen'
>>> 
>>> str(myunicode)
'xmen'
>>> 
>>> unicode("xmen")
u'xmen'
>>> 

To convert a unicode string into regular string use str() function, to convert a regular string into unicode use unicode() function.

String data type is an immutable object
1. Immutable means cannot be changed.

2. Recall that a variable is only a reference to an object. We can change the reference of an object but we cannot change the object residing in the memory.


>>> fruits
'apple'
>>> 
>>> fruits[0]
'a'
>>> fruits[1]
'p'
>>> fruits[0] = p
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'p' is not defined
>>> 

fruits is the variable that refers to the object apple, but as seen above i cannot modify parts of the string. The only way to change it is to use variable fruits to reference to another object.


>>> fruits = "orange"
>>> fruits
'orange'
>>> 

fruits only changes the reference of an object from apple to orange however the object apple still exists.


>>> fruits = "apple"
>>> favFruits = fruits
>>> favFruits
'apple'
>>> fruits
'apple'
>>> 

What happened here is that variable fruits refers to object apple, and variable favFruits refers to the variable fruits which also refers to apple.


>>> fruits = "orange"
>>> fruits
'orange'
>>> favFruits
'apple'
>>> 

As can be seen above, although variable fruits has changed the reference to orange, the object apple still exists.

Concatenate strings

>>> fname = "cyrus"
>>> lname = "lok"
>>> fullname = fname + ' ' + lname
>>> fullname
'cyrus lok'
>>> print fullname
cyrus lok
>>> 
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s