You are on page 1of 10

In [47]: # (These are the notes made in the MSWORD Doc that are pasting here in the Jupyter

notebook.)

# [24 May 2018]


# 1.Section 2 Lecture 14 – Indexing & Slicing
# 1.“hello”
# 2.‘hello’
# 3.print (“hello”)
# 4.print (“Hi \nI am”) – new line escape sequence \n
# 5.print (“Hi \tI am”) – Fo TAB we use \t
# 6.len(“hello”)
# 7 .(“hello”)[2] on run we get value l
# As string is a sequence of character so we can select the character that we want according to its indexin
g position
# 0 1 2 3 4
# H e l l o
# We have the concept of reverse Index in in which we can get the value of last character of string no mat
ter how long the string was.
# 2.Section 3 Lecture 15 – Indexing & Slicing
# Indexing : Here we choose particular character from string by calling its numerical position in the strin
g
# 1.my string = “ABCDEFGHIJK”
# 2. my string[2] - > C
# 3my string[4] -> E
# 4my string[-1] -> K This is called reverse string and we get the last character of the string through t
his
# Slicing : Here we took sub-section of the String Synatx [start:stop:step]
# 5my string[2:] -> “CDEFGHIJK” It means that from the position of 2nd letter to the end of
# 6my string[:3] - > This means include string character from beginning to the character before mentioned n
umber for e.g here 3 is mentioned so that the character should be from 0 ,1 & 2 which is “ABC”
# 7 suppose we have to slice DEF from the string “ABCDEFGHIJK” my string[3:6]
# 8 suppose we have to slice GH from the string “ABCDEFGHIJK” my string[6:8]
# 9. my string [::2] -> This means from the start to the end of the string select character after 2 interva
ls starting from the 0 position.
# String Properties and Methods
# 1.String Immutability : What it means that you cannot change the string in manner like
# String = “SAM” & you want to change thw name like “PAM” change S to P with code something like this
# String[0] = “P” you will get error for this whaich is string doesnot support item assignment.
# Although some other data type support this concept.
# Now how can we achieve this it’s through the Concatenation concept.
# So we do the following
# name[0] = “P” ---- This is used as for a comment.
# Last_letters = String[1:]
# “P ” + Last_letters
# 2. x = "Hi There "
# y = "Where are you"
# x + y result 'Hi There Where are you'

# 3. x = "Hi there"
# x = x + " you are good"
# 'Hi there you are good you are good you are good'
# 4.Multiplication of the string
# Letter = “z”
# Letter * 10 result = zzzzzzzzzz
# 5.Remember that when we do addition like 2 + 3 results = 5
# But if do like this “2” + “3” result = “23” it works like the string concatenation not the addition of th
e string so this is the flaw of dynamic typing of python we have to be careful what data type are there and
what will be the output for them.
# 6. Builtin String Methods : So objects in pyhton usually have built in methods and these methods itself a
re essentially functions inside the objects.
# Later we built our own functions and Methods.)

In [1]: X = "SAM"

In [2]: X

Out[2]: 'SAM'

In [3]: X[0] = "P"

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-7f917f4e3eb6> in <module>()
----> 1 X[0] = "P"

TypeError: 'str' object does not support item assignment

In [5]: y = X[1:]

In [6]: "P" + y

Out[6]: 'PAM'

In [ ]: # This is called Concatenation of the String


In [17]: x = "Hi There "

In [14]: y = "Where are you"

In [18]: x + y

Out[18]: 'Hi There Where are you'

In [25]: x = "Hi there"

In [26]: x

Out[26]: 'Hi there'

In [32]: x = x + " you are good"

In [33]: x

Out[33]: 'Hi there you are good you are good you are good'

In [34]: # this is the comment

In [35]: x= 'Hello World How are you'

In [36]: x

Out[36]: 'Hello World How are you'

In [45]: x.upper()

Out[45]: 'HELLO WORLD HOW ARE YOU'

In [38]: x.lower()

Out[38]: 'hello world how are you'


In [44]: x.split()

Out[44]: ['Hello', 'World', 'How', 'are', 'you']

In [43]: x.split('o')

Out[43]: ['Hell', ' W', 'rld H', 'w are y', 'u']

In [48]: #Strings -FAQ


#Section 3, Lecture 17

# 1. Are strings mutable?

# Strings are not mutable! (meaning you can't use indexing to change individual elements of a string)

# 2. How do I create comments in my code?

# You can use the hashtag # to create comments in your code

In [49]: # Section 3 Lecture 18


# Print Formatting with the Strings

In [ ]: # Often you will want to “inject” a variable into your string for printing. For example:
# my_name = “Jose”
# print(“Hello ” + my_name)
# There are multiple ways to format strings for printing variables in them.
# This is known as STRING INTERPELLATION this is fancy way of saying that insert a variable into the String
# Let’s explore two methods for this:
# 1.format() method
# 2.f-strings (formatted string literals)

In [9]: V1 = " Shakeeb"

In [2]: V1

Out[2]: 'Shakeeb'

In [10]: print('Hi' + V1)

Hi Shakeeb
In [21]: # Now there's actually multiple ways to format strings for printing variables in them so you don't have
#
# to constantly be using this concatenation or plus sign.
#
# And this in general is known as string interpellation which is basically just a really fancy way of
#
# saying stick a variable into a string so we're going to explore two methods for doing this.
#
# One is the format method [.format] like 'String here {}then also {}'.format('something1','something
2') and the other one
# is the strings method which stands for formatted string literals[f-strings]

In [ ]: # Let's begin by discussing the format method and the basic syntax for this is you're going to have you
r
#
# string defined and then inside of your string you're going to have special curly braces as placeholde
rs
#
# for the variables you're going to insert and then write up the string you call that format.
#
# And then inside of this you're going to call the strings or variables that you want to insert into yo
ur
#
# string.
In [ ]: # Let's walk through lots of examples here.
#
# We're going start from the most basic example which is just saying this is a string going to open a
nd
#
# close curly braces there.
#
# And right after the string we're going to call that format.
#
# So notice how the dot is touching the string there and then whatever string you want to insert.
#
# So to make it really obvious I'm gonna go put in all caps inserted run this and I see this is a str
ing
#
# inserted.
#
# So with the DOT format method has done is it's grab the string and insert it where it saw those cur
ly
#
# braces.
#
# So there's several advantages here and we're going to go through all of them.
#
# One advantage is that strings can actually be inserted by index position.

In [25]: print('This is String {}'.format('INSERTED'))

This is String INSERTED


In [ ]: # Let's imagine that we want to insert many things.
#
# We're going say the curly braces curly braces curly braces.
#
# Then say that format and say the Fox
#
# Brown.
#
# Now what happens is basically that format is going to insert the strings in the same order.
#
# You supplied them and into these curly braces.
#
# So right now we have the fox Brown quick.

In [26]: print('The {} {} {}'.format('fox','brown','quick'))

The fox brown quick

In [ ]: # Here we give the index postion in the string for the variables define in format method

In [28]: print('The {0} {1} {2}'.format('fox','brown','quick'))

The fox brown quick

In [ ]: # Here we call the index value 0 for the the braces then we get this result

In [29]: print('The {0} {0} {0}'.format('fox','brown','quick'))

The fox fox fox

In [ ]: # Here we define the keywords and call them in the string which are similar like the variable names

In [30]: print('The {z} {y} {x}'.format(x='fox',y='brown',z='quick'))

The quick brown fox

In [ ]: # FLOAT FORMATTING follows "{value:width.precision f}"


# Lets see with the exampe
In [31]: Result = 1000/777

In [35]: Result

Out[35]: 1.287001287001287

In [37]: print('The result is {r}'.format(r=Result))

The result is 1.287001287001287

In [ ]: # Now with the help of the floating formatting we can reduce the level of precision that we required along wi
th the width given

In [38]: print('The result is {r:1.3f}'.format(r=Result))

The result is 1.287

In [39]: #FORMAT STRING LITERAL f string this is the new method available in the Python 3.6
# Here what we do show by example

In [40]: name = "Shakeeb"

In [41]: print("My name is {}".format(name))

My name is Shakeeb

In [43]: print(f"My name is {name}")

My name is Shakeeb

In [7]: name = "Leena"


Age = 20

In [8]: print(f'{name}, is {Age} years old')

Leena, is 20 years old


In [9]: # Print Formatting FAQs
# Section 3, Lecture 19
# Print Formatting FAQS
#
# 1.) I imported print from the __future__ module, now print isn't working. What happened?
#
# This is because once you import from the __future__ module in Python 2.7, a print statement will no
longer work, and print must then use a print() function. Meaning that you must use
#
# print('Whatever you were going to print')
#
# or if you are using some formatting:
#
# print('This is a string with an {p}'.format(p='insert'))
#
# The __future__ module allows you to use Python3 functionality in a Python2 environment, but some fu
nctionality is overwritten (such as the print statement, or classic division when you import division).
#
# Since we are using Jupyter Notebooks, once you so the import, all cells will require the use if the
print() function. You will have to restart Python or start a new notebook to regain the old functionality
back.
#
# HERE IS AN AWESOME SOURCE FOR PRINT FORMATTING:
#
# https://pyformat.info/

You might also like