Strings text is most common form of data in our daily

Info icon This preview shows pages 17–23. Sign up to view the full content.

Strings Text is most common form of data in our daily life In Python, they are stored as a sequence of characters internally The length of a string can be varied during program execution Use single quote ‘ or double quote “ to delimit CSCI2040 Introduction to Python 17
Image of page 17

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Strings Enclosed either by single quote ‘ or double quote “ >>> 'doesn\'t‘ "doesn't" >>> "doesn't" "doesn't" >>> '"Yes," \nhe said.‘ "Yes," \nhe said.‘ >>> print ('"Yes," \nhe said.’“) Yes," he said. >>> "\"Yes,\" he said.“ '"Yes," he said.' >>> '"Isn\'t," she said.' '"Isn\'t," she said.' Use \ to tell not delimit character, \ is called escape char \n move the cursor to next line, a.k.a newline character Another way to print “ when delimit is also “ CSCI2040 Introduction to Python 18
Image of page 18
Strings Concatenated or repeated with + and * respectively >>> word = ‘SEEM' + 'super‘ >>> word ' SEEMsuper‘ >>> '['+word*3 + ']‘ '[SEEMsuper SEEMsuper SEEMsuper]' CSCI2040 Introduction to Python 19
Image of page 19

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Strings Access can be in subscripted notation >>> word[2] ‘E‘ substrings can be specified with the slice notation >>> word[0:2] ‘SE‘ >>> word[:2] ‘SE‘ >>> word[1:] ‘EEMsuper' For non-negative indices, the length of a slice is the difference of the indices, if both are within bounds CSCI2040 Introduction to Python 20
Image of page 20
Strings Immutable – can’t be modified once created >>> word[0] = 2 Traceback (most recent call last): File "<pyshell#14>", line 1, in <module> word[0] = 2 TypeError: 'str' object does not support item assignment But we can create a new string in this case >>> word[0:3]+ “ course” ‘SEE course' CSCI2040 Introduction to Python 21
Image of page 21

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

String functions Operation Description s.capitalize() capitalize the first character of s s.capwords() capitalize the first letter of each word in s s.count(sub) count number of occurrence of sub in s s.find(sub) find first index of sub in s, or -1 if not found s.index(sub) find first index of sub in s, or raise a ValueError if not found s.rfind(sub) find last index of sub in s, or -1 if not found s.rindex(sub) find last index of sub in s, or raise a ValueError if not found s.lower() convert s to lowercase s.split() return a list of words in s s.join(lst) join a list of words into a single string with s as separator s.strip() strips leading/trailing white space from s s.upper() convert s to upper case s.replace(old,new)replace all instances of old with new in string Note the use of ‘.’ to call the string function CSCI2040 Introduction to Python 22
Image of page 22
Image of page 23
This is the end of the preview. Sign up to access the rest of the document.
  • Fall '17
  • Letter case, Lower case

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern