0% found this document useful (0 votes)
71 views19 pages

Python Alpha 101 B: Strings

Python Programming

Uploaded by

Abdul Aziz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views19 pages

Python Alpha 101 B: Strings

Python Programming

Uploaded by

Abdul Aziz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Python Alpha 101 b

Strings

AL NAFI,
A company with a focus on education,
wellbeing and renewable energy.

© 2018 Al-Nafi. All Rights Reserved. 1


Dua of the day to recite after Takbeer in Salah

Al-Bukhari 1/181, Muslim 1/419


© 2018 Al-Nafi. All Rights Reserved. 2
Study, Rinse and Repeat
• Please subscribe to our YouTube Channel to be on top of your studies.
• Please logon to our website https://alnafi.com/login/
• Use your username and password to logon
• Please keep an eye on [email protected] emails
• Please review the videos of 30 minutes daily
• Please review the notes daily.
• Please take time for clearing up your mind and reflect on how things
are proceeding in your studies.

© 2018 Al-Nafi. All Rights Reserved. 3


String Slicing
String Slicing is used a lot in real life. Within Machine Learning, Deep
Learning, Data Wrangling, AI, website, Games you name it!!

my_string = "My Country Pakistan"


my_string[0:0]

We can even use

my_string = "My Country Pakistan"


my_string[0:-3]
© 2018 Al-Nafi. All Rights Reserved. 4
Slicing down to a single character
my_string = "My Country Pakistan"
print(my_string[0])

© 2018 Al-Nafi. All Rights Reserved. 5


String Slicing has real usage
As mentioned string slicing is used in ML, DL, AI, DS and big data for
parsing fixed width records etc along with other use cases.

Especially text file processing

Now we are increasing our understanding and moving slowly towards


ML, DL, AI and other emerging fields using Python 

© 2018 Al-Nafi. All Rights Reserved. 6


String Formatting AKA substitution
String formatting (AKA substitution) is the topic of substituting values
into a base string. Most of the time, you will be inserting strings within
strings;

However you will also find yourself inserting integers and floats into
strings quite often as well. There are two different ways to accomplish
this task. We'll start with the old way of doing things and then move on
to the new.

© 2018 Al-Nafi. All Rights Reserved. 7


The old way of string substitution step 1
my_string = "I like %s" % "Python"

my_string

© 2018 Al-Nafi. All Rights Reserved. 8


The old way of string substitution step 2
var = "cookies"
newString = "I like %s" % var
Note:
newString The %s is the important piece in the code
as it tells Python that we may be inserting
text soon. If we follow the string with a
percent sign and another string or variable,
then Python will attempt to insert it into the
string. We can insert multiple strings by
putting multiple instances of %s inside our
string. You'll see that in the last example. Just
note that when you insert more than one
string, you have to enclose the strings that
you're going to insert with parentheses.

© 2018 Al-Nafi. All Rights Reserved. 9


The old way of string substitution step 3
Note:
var = "cookies" The %s is the important piece in
the code
another_string = "I like %s and %s" % ("Python", var) as it tells Python that we may ne
inserting text soon. If we follow
another_string the string with a percent sign and
another string or variable, then
Python will attempt to insert it
into the string. We can insert
multiple strings by putting
multiple instances of %s inside
our string. You'll see that in the
last example. Just note that when
you insert more than one string,
you have to enclose the strings
that you're going to insert with
parentheses.
© 2018 Al-Nafi. All Rights Reserved. 10
Examples with integers
my_string = "%i + %i = %i" % (1,2,3)
my_string

© 2018 Al-Nafi. All Rights Reserved. 11


Examples with float
float_string = "%f" % (1.23)
float_string

Or another float example

float_string2 = "%.2f" % (1.23)


float_string2

© 2018 Al-Nafi. All Rights Reserved. 12


Templates and the New String Formatting
Methodology
Another way of formatting

print("%(lang)s is fun!" % {"lang":"Python"})

This probably looks pretty weird, but basically we just changed our %s into
%(lang)s, which is basically the %s with a variable inside it. The second part is
actually called a Python dictionary that we will be studying in the next class.
Basically it's a key:value pair, so when Python sees the key "lang" in the
string AND in the key of the dictionary that is passed in, it replaces that key
with its value.
© 2018 Al-Nafi. All Rights Reserved. 13
More Examples with text
print("%(value)s %(value)s %(value)s !" % {"value":"Sharfoo"})

© 2018 Al-Nafi. All Rights Reserved. 14


More examples with number
print("%(x)i + %(y)i = %(z)i" % {"x":1, "y":2, "z":3})

© 2018 Al-Nafi. All Rights Reserved. 15


Another example
"Python is as simple as {0}, {1}, {2}".format("a", "b", "c")

© 2018 Al-Nafi. All Rights Reserved. 16


Another example
xy = {"x":0, "y":10}
print("Graph a point at where x={x} and y={y}".format(**xy))

© 2018 Al-Nafi. All Rights Reserved. 17


Python Official Documentation
https://docs.python.org/3/

String Formatting https://docs.python.org/3/library/string.html#string-


formatting

More on String Formatting


https://docs.python.org/3/library/string.html#formatexamples

© 2018 Al-Nafi. All Rights Reserved. 18


‫ه‬ ‫للا‬ ‫جزاك‬
To ask questions, please logon to the portal https://alnafi.com/login/
and use your username and password. From within the portal you can
ask questions. We will only answer questions if they are coming
through the portal and not on email.

For any other queries please reach out on [email protected]

© 2018 Al-Nafi. All Rights Reserved. 19

You might also like