Python Notes-3

In continuation from Python Notes-2
Python Notes-1

We will learn about python string as you know string in python are immutable all function which applies to string will eventually not modify the string but will return a str e.g.

var = 'ab'
can’t change str but returns another string

sometime I just forget this and assume that my original variable is changed to all upper, silly mistake.

var = var.lower()

Count number of times a value is appearing in str or tuple or list
Index of a value

To include special characters in a string you can escape using \ for or just use another for quotation then you can use one inside without escaping.
If you want to be more specific you can place r before the beginning quotation mark of a string to make it a raw string. A raw string completely ignores all escape characters and prints any backslash that appears in the string. e.g.

>>> print(r'This!! This is me \' " cat.')
This!! This is me \' " cat.

Multiline Strings with Triple Quotes

print('''Dear Apple,

You are so sweet.


Dear Apple,

You are so sweet.


After reading about str, list and tuple I wanted to know why use tuple? Why another data structure?

Seems like python uses tuple behind our back when a function has multiple return type. Even when it is not specifically mentioned.
Also since tuple data structure is restrictive it is much faster compared to list. There is one more benefit which relates to code understanding if you use tuple data type in your code for variables which doesn’t need to be changed. It will be known to reader without you telling specifically.

Just like string concatenation list and tuple can also be concatenated. All three cal be multiplied/replicated

t1 = (1,2)
t2 = (3,4)
t3 = t1+t2


t4 = t1*4

To be continued …


9 thoughts on “Python Notes-3

Leave a Reply

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

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

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s