Fundamentals of Programming (Python)ce.sharif.edu/courses/96-97/1/ce153-12/resources/root/Slides/6....
Transcript of Fundamentals of Programming (Python)ce.sharif.edu/courses/96-97/1/ce153-12/resources/root/Slides/6....
Fundamentals of Programming(Python)
Strings
Sina SajadmaneshSharif University of Technology
Fall 2017Some slides have been adapted from “Python Programming: An Introduction to
Computer Science”
Outline1. String Data Type
2. String Representation
3. String Indexing
4. Operations on Strings
5. String Methods
2SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017
String Data TypeText is represented in programs by the string data type
A string is a immutable sequence of charactersenclosed within quotation marks (") or apostrophes (').◦ Example:
◦ ‘This is a sample sentence.’
◦ “2x2=4”
3SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017
String RepresentationCharacter Encoding◦ A string is stored as a sequence of binary numbers,
one number per character
◦ The mapping used to convert characters to numbers and vice-versa is called character encoding
◦ Python 3 uses Unicode
4SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017
>>> ord('a') # Return the Unicode code point for a one-character string.
97
>>> chr(97) # Return a Unicode string of one character with ordinal i
'a'
String Indexing
5SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017
>>> greet = "Hello Bob"
>>> greet[0]
'H'
>>> print(greet[0], greet[2], greet[4])
H l o
>>> x = 8
>>> print(greet[x - 2])
B
>>> greet[-1]
'b'
>>> greet[-3]
'B'
H e l l o B o b
0 1 2 3 4 5 6 7 8
String Indexing
6SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017
H e l l o B o b
0 1 2 3 4 5 6 7 8
>>> greet[0:3]
'Hel'
>>> greet[5:9]
' Bob'
>>> greet[:5]
'Hello'
>>> greet[5:]
' Bob'
>>> greet[:]
'Hello Bob'
Operations on StringsTraversal:
7SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017
fruit = 'banana'
n = len(fruit)
for i in range(n):
c = fruit[i]
print(c)
Output:
b
a
n
a
n
a
fruit = 'banana'
for c in fruit:
print(c)
Output:
b
a
n
a
n
a
Operations on StringsComparison:
8SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017
Operations on StringsStrings are immutable
9SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017
Operations on StringsThe in Operator
10SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017
String Methods◦ s.capitalize() – Copy of s with only the first
character capitalized
◦ s.title() – Copy of s; first character of each word capitalized
◦ s.center(width) – Center s in a field of given width
◦ s.count(sub) – Count the number of occurrences of sub in s
◦ s.find(sub) – Find the first position where sub occurs in s
◦ s.join(list) – Concatenate list of strings into one large string using s as separator.
11SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017
String Methods◦ s.ljust(width) – Like center, but s is left-justified
◦ s.lower() – Copy of s in all lowercase letters
◦ s.lstrip() – Copy of s with leading whitespace removed
◦ s.replace(oldsub, newsub) – Replace occurrences of oldsub in s with newsub
◦ s.rfind(sub) – Like find, but returns the right-most position
◦ s.rjust(width) – Like center, but s is right-justified
12SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017
String Methods◦ s.rstrip() – Copy of s with trailing whitespace
removed
◦ s.split() – Split s into a list of substrings
◦ s.upper() – Copy of s; all characters converted to uppercase
For more, refer to:https://docs.python.org/3/library/stdtypes.html#string-methods
13SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017