Fu Fundamentals of Pr Programming (Py Python) Strings Ali Taheri Sharif University of Technology Spring 2019 Some slides have been adapted from “Python Programming: An Introduction to Computer Science”
Outline 1. String Data Type 2. String Representation 3. String Indexing 4. Operations on Strings 5. String Methods 2 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
String Data Type Text is represented in programs by the string data type A string is a immutable sequence of characters enclosed within quotation marks (") or apostrophes ('). ◦ Example: ◦ ‘This is a sample sentence.’ ◦ “ 2x2=4 ” 3 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
String Representation Character 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 >>> 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' 4 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
String Indexing H e l l o B o b 0 1 2 3 4 5 6 7 8 >>> 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' 5 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
String Indexing 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' 6 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Operations on Strings Traversal: fruit = 'banana' fruit = 'banana' n = len(fruit) for c in fruit: for i in range(n): print(c) c = fruit[i] print(c) Output: Output: b b a a n n a a n n a a 7 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Operations on Strings Comparison: 8 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Operations on Strings Strings are immutable 9 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Operations on Strings The in Operator 10 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
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. 11 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
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 12 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
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 13 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Recommend
More recommend