Saturday, February 11, 2017

Python Strings - Part1

Strings are very common object types every developer or coder deal with. There is a free flow of
strings at every step of code block, that needs to be manipulated or used. Some times, one would like to parse data and perform a boolean operation.

Thanks to python, unlike any other programming languages, variables need not to be declared ahead. Python automatically finds a variable by its type.

For instance:

s = 'this is a test programme'
type(s)
Output: str
But here is the catch, python can automatically adopt the changes to variable type as the assigned value changes. Meaning, a string may change to a number and so on.

In Python we can assign a string to a variable using a single('), double (") and triple quote("'). Lets assign '2' as a string to variable 's'.

s = '2'
type(s)
Output: str
Now convert the string to an integer and assign it to 's'. In other languages, it gives an error, but not in Python.

s = int(s)
type(s)
Output: int
As you now see, the type of variable has been changed from string to integer.

As mentioned earlier, we can assign strings to a variable using single, double and triple quotes. Now the question is when and where to use them.

Single quote and double quotes does the same thing, but we can use them wisely.

For instance, look at the example below, both achieve the same result. Let's look at an example with single quotes

 s = 'Python was implemented by Guido van Rossum'
print s
Output: Python was implemented by Guido van Rossum

Now look at the same example with double quotes.

 s = "Python was implemented by Guido van Rossum"
print s

Output: Python was implemented by Guido van Rossum

But, they make difference in some implementations. For instance, one may choose to use single quotes, when the text contains double quotes.
 s = 'Python was implemented by "Guido van Rossum"'
print s
Output: Python was implemented by "Guido van Rossum"

and use double quote, when the text contains single quote.

 s = "Python was implemented by 'Guido van Rossum'"
print s
Output: Python was implemented by 'Guido van Rossum'

Triple quotes in python are useful to enter blocks of data, now the triple quotes can be of ''' or """. Look at the example below.

 s = """About the origin of Python, Van Rossum wrote in 1996:\n
Over six years ago, in December 1989, I was looking for
a "hobby" programming project that would keep me occupied
during the week around Christmas. My office ... would be closed,
but I had a home computer, and not much else on my hands. I decided
to write an interpreter for the new scripting language I had been 
thinking about lately: a descendant of ABC that would appeal to 
Unix/C hackers. I chose Python as a working title for the project, 
being in a slightly irreverent mood 
(and a big fan of Monty Python's Flying Circus)."""
print s


And print below.

Output: About the origin of Python, Van Rossum wrote in 1996:
Over six years ago, in December 1989, I was looking for
a "hobby" programming project that would keep me occupied
during the week around Christmas. My office ... would be closed,
but I had a home computer, and not much else on my hands. I decided
to write an interpreter for the new scripting language I had been
thinking about lately: a descendant of ABC that would appeal to
Unix/C hackers. I chose Python as a working title for the project,
being in a slightly irreverent mood
(and a big fan of Monty Python's Flying Circus).