introduction to programming with python-3

17
Introduction to Programming with Python – Class 3 SYED FARJAD ZIA ZAIDI

Upload: syed-farjad-zia-zaidi

Post on 26-May-2015

212 views

Category:

Software


3 download

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

Page 1: Introduction To Programming with Python-3

Introduction to Programming with Python – Class 3SYED FARJAD ZIA ZAIDI

Page 2: Introduction To Programming with Python-3

Class Objectives

Class Objective

Understanding Strings and some useful string operations.

Write few simple programs that use string functions and lists.

Page 3: Introduction To Programming with Python-3

Class Material

• Chapter 6, 8 - Python for Informatics: Exploring Information

Reading

Page 4: Introduction To Programming with Python-3

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”

Page 5: Introduction To Programming with Python-3

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]

Page 6: Introduction To Programming with Python-3

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

Page 7: Introduction To Programming with Python-3

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

Page 8: Introduction To Programming with Python-3

String Special Operators

+

• Concatenation - Adds values on either side of the operator

*• Repetition -

Creates new strings, concatenating multiple copies of the same string

Page 9: Introduction To Programming with Python-3

Python Comparison Operators

• Slice - Gives the character from the given index

[]

• Range Slice - Gives the characters from the given range

[ : ]

Page 10: Introduction To Programming with Python-3

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

Page 11: Introduction To Programming with Python-3

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.

Page 12: Introduction To Programming with Python-3

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

Page 13: Introduction To Programming with Python-3

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)

Page 14: Introduction To Programming with Python-3

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";

Page 15: Introduction To Programming with Python-3

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'

Page 16: Introduction To Programming with Python-3

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'

Page 17: Introduction To Programming with Python-3

Any Queries?

Link to Facebook Group:

https://www.facebook.com/groups/introtopython.iccbs/

Email:

[email protected]