Strings Ali Taheri Sharif University of Technology Spring 2019 - - PowerPoint PPT Presentation

strings
SMART_READER_LITE
LIVE PREVIEW

Strings Ali Taheri Sharif University of Technology Spring 2019 - - PowerPoint PPT Presentation

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.


slide-1
SLIDE 1

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”

slide-2
SLIDE 2

Outline

  • 1. String Data Type
  • 2. String Representation
  • 3. String Indexing
  • 4. Operations on Strings
  • 5. String Methods

2

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-3
SLIDE 3

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

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-4
SLIDE 4

String Representation

Character Encoding

  • A string is stored as a sequence of binary numbers,
  • ne number per character
  • The mapping used to convert characters to numbers

and vice-versa is called character encoding

  • Python 3 uses Unicode

4

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

>>> 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'

slide-5
SLIDE 5

String Indexing

5

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

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

  • B
  • b

0 1 2 3 4 5 6 7 8

slide-6
SLIDE 6

String Indexing

6

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

H e l l

  • B
  • 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'

slide-7
SLIDE 7

Operations on Strings

Traversal:

7

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

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

slide-8
SLIDE 8

Operations on Strings

Comparison:

8

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-9
SLIDE 9

Operations on Strings

Strings are immutable

9

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-10
SLIDE 10

Operations on Strings

The in Operator

10

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-11
SLIDE 11

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
  • ccurs in s
  • s.join(list) – Concatenate list of strings into one

large string using s as separator.

11

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-12
SLIDE 12

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
  • f 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

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-13
SLIDE 13

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

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019