introduction to programming with python-3
DESCRIPTION
Slides for Lecture 3 of the course: Introduction to Programming with Python offered at ICCBS. It covers the following topics: Strings useful string operations.TRANSCRIPT
Introduction to Programming with Python – Class 3SYED FARJAD ZIA ZAIDI
Class Objectives
Class Objective
Understanding Strings and some useful string operations.
Write few simple programs that use string functions and lists.
Class Material
• Chapter 6, 8 - Python for Informatics: Exploring Information
Reading
Strings
Strings are amongst the most popular types in Python. We can create them simply by enclosing characters in quotes. Python treats single quotes the same as double quotes.
Creating strings is as simple as assigning a value to a variable. For example:
message = “Hello World”
Introduction to Indexing Strings can be divided into substrings using indexing.
To access substrings, use the square brackets for slicing along with the index or indices to obtain your substring, this is called Indexing. Following is a simple example:
message = 'Hello World!'
messageTwo = "Python Programming"
print "var1[0]: ", message[0]
print "var2[1:5]: ", messageTwo[1:5]
String Concatenation
String concatenation is a process in which two strings are merged together. Concatenation is very simple and uses the operator ‘+’ to concatenate.
For Example:
message = “Hello”
messageTwo = “ “
messageThree = “World”
print message + messageTwo + messageThree
Escape Characters
Following table is a list of commonly used escape or non-printable characters that can be represented with backslash notation.
An escape character gets interpreted; in a single quoted as well as double quoted strings.Backslash Notation Description
\n New Line
\t Space
\s Tab
String Special Operators
+
• Concatenation - Adds values on either side of the operator
*• Repetition -
Creates new strings, concatenating multiple copies of the same string
Python Comparison Operators
• Slice - Gives the character from the given index
[]
• Range Slice - Gives the characters from the given range
[ : ]
Python Comparison Operators
• Membership - Returns true if a character exists in the given string
in
• Membership - Returns true if a character does not exist in the given string
not in
Python Triple Quotes
Python's triple quotes comes to the rescue by allowing strings to span multiple lines, including verbatim NEWLINEs, TABs, and any other special characters.
String Functions:Method Description
capitalize() Capitalizes first letter of string
count(str, beg=0, end=len(string)) Counts how many times str occurs in string or in a substring of string if starting index beg and ending index end are given
find(str, beg=0, end=len(string)) Determine if str occurs in string or in a substring of string if starting index beg and ending index end are given returns index if found and -1 otherwise
index(str, beg=0, end=len(string)) Same as find(), but raises an error if str not found
len(string) Returns the length of the string
String Functions:
Go to the following link for more string functions:
http://www.tutorialspoint.com/python/python_strings.htm
You can also find string functions by writing the following in the python IDLE
help(str)
Exercise 1
Write code using find() and string slicing to extract the number at the end of the line below. Convert the extracted value to a floating point number and print it out.
text = "X-DSPAM-Confidence: 0.8475";
Exercise 2 Given a string and a non-negative int n, return a larger string that is n
copies of the original string.
string_times('Hi', 2) → 'HiHi'string_times('Hi', 3) → 'HiHiHi'string_times('Hi', 1) → 'Hi'
Exercise 3 Given a string, return a string where for every char in the original,
there are two chars.
double_char('The') → 'TThhee'
double_char('AAbb') → 'AAAAbbbb'
double_char('Hi-There') → 'HHii--TThheerree'
Any Queries?
Link to Facebook Group:
https://www.facebook.com/groups/introtopython.iccbs/
Email: