Professional Documents
Culture Documents
notebook.)
# 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'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-7f917f4e3eb6> in <module>()
----> 1 X[0] = "P"
In [5]: y = X[1:]
In [6]: "P" + y
Out[6]: 'PAM'
In [18]: x + y
In [26]: x
In [33]: x
Out[33]: 'Hi there you are good you are good you are good'
In [36]: x
In [45]: x.upper()
In [38]: x.lower()
In [43]: x.split('o')
Out[43]: ['Hell', ' W', 'rld H', 'w are y', 'u']
# Strings are not mutable! (meaning you can't use indexing to change individual elements of a string)
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 [2]: V1
Out[2]: 'Shakeeb'
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 [ ]: # Here we give the index postion in the string for the variables define in format method
In [ ]: # Here we call the index value 0 for the the braces then we get this result
In [ ]: # Here we define the keywords and call them in the string which are similar like the variable names
In [35]: Result
Out[35]: 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 [39]: #FORMAT STRING LITERAL f string this is the new method available in the Python 3.6
# Here what we do show by example
My name is Shakeeb
My name is Shakeeb