LECTURE 5 Strings. STRINGS We’ve already introduced the string data type a few lectures ago. Strings are subtypes of the sequence data type. Strings are.

Slides:



Advertisements
Similar presentations
Computer Science & Engineering 2111 Text Functions 1CSE 2111 Lecture-Text Functions.
Advertisements

CS 100: Roadmap to Computing Fall 2014 Lecture 0.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Strings. Strings are amongst the most popular types in Python. We can create them simply by enclosing characters in quotes. Python treats single quotes.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
 Pearson Education, Inc. All rights reserved Formatted Output.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
Python for Informatics: Exploring Information
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 12 More about strings 05/02/09 Python Mini-Course: Day 3 – Lesson 12.
Input, Output, and Processing
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Built-in Data Structures in Python An Introduction.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Chapter 5 Strings CSC1310 Fall Strings Stringordered storesrepresents String is an ordered collection of characters that stores and represents text-based.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! We have agreed so far that it can.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
More Strings CS303E: Elements of Computers and Programming.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
INLS 560 – S TRINGS Instructor: Jason Carter. T YPES int list string.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
String Methods Programming Guides.
Chapter 8 Text Files We have, up to now, been storing data only in the variables and data structures of programs. However, such data is not available.
Containers and Lists CIS 40 – Introduction to Programming in Python
String Processing Upsorn Praphamontripong CS 1110
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Introduction to Scripting
Lecture 5 Strings.
Chapter 12: Text Processing
Winter 2018 CISC101 11/16/2018 CISC101 Reminders
Chapter 12: Text Processing
Basic Python Collective variables (sequences) Lists (arrays)
Topics Introduction to File Input and Output
2.1 Parts of a C++ Program.
Bryan Burlingame 17 October 2018
String Manipulation Chapter 7 Attaway MATLAB 4E.
String and Lists Dr. José M. Reyes Álamo.
Basic String Operations
Methods – on strings and other things
Chapter 2: Introduction to C++.
CS 1111 Introduction to Programming Spring 2019
15-110: Principles of Computing
Topics Basic String Operations String Slicing
Bryan Burlingame 13 March 2019
Text Manipulation Chapter 7 Attaway MATLAB 5E.
Python Strings.
Topics Basic String Operations String Slicing
Topics Introduction to File Input and Output
Topics Basic String Operations String Slicing
STRING MANUPILATION.
Presentation transcript:

LECTURE 5 Strings

STRINGS We’ve already introduced the string data type a few lectures ago. Strings are subtypes of the sequence data type. Strings are written with either single or double quotes encasing a sequence of characters. Note that there is no character data type in Python. A character is simply represented as a string with one character. s1 = "This is a string!" s2 = "Python is so awesome."

ACCESSING STRINGS As a subtype of the sequence data type, strings can be accessed element-wise as they are technically just sequences of character elements. We can index with typical bracket notation, as well as perform slicing. >>> s1 = "This is a string!" >>> s2 = "Python is so awesome." >>> print s1[3] s >>> print s2[5:15] n is so aw

MODIFYING STRINGS Strings are immutable – you cannot update the value of an existing string object. However, you can reassign your variable name to a new string object to perform an “update”. >>> s1 = "Python is so awesome." >>> s1 = "Python is so cool." “Python is so awesome.” s1 “Python is so awesome.” “Python is so cool.”

MODIFYING STRINGS Alternatively, we could have done the following: This will create a substring “Python is so ”, which is concatenated with “cool.”, stored in memory and associated with the name s1. The “+” operator can be used with two string objects to concatenate them together. The “*” operator can be used to concatenate multiple copies of a single string object. We also have in and not in available for testing character membership within a string. >>> s1 = "Python is so awesome." >>> s1 = s1[:13] + "cool."

ESCAPE CHARACTERS As a side note, there are a number of escape characters supported by Python strings. The most common ones are: ‘\n’ – newline ‘\s’ – space ‘\t’ – tab

BUILT-IN STRING METHODS Python includes a number of built-in string methods that are incredibly useful for string manipulation. Note that these return the modified string value; we cannot change the string’s value in place because they’re immutable! s.upper() and s.lower() >>> s1 = "Python is so awesome." >>> print s1.upper() PYTHON IS SO AWESOME. >>> print s1.lower() python is so awesome.

BUILT-IN STRING METHODS s.isalpha(), s.isdigit(), s.isalnum(), s.isspace() – return True if string s is composed of alphabetic characters, digits, either alphabetic and/or digits, and entirely whitespace characters, respectively. s.islower(), s.isupper() – return True if string s is all lowercase and all uppercase, respectively. >>> "WHOA".isupper() True >>> "12345".isdigit() True >>> " \n ".isspace() True >>> "hello!".isalpha() False

BUILT-IN STRING METHODS str.split([sep[, maxsplit]]) – Split str into a list of substrings. The sep argument indicates the delimiting string (defaults to consecutive whitespace). The maxsplit argument indicates the maximum number of splits to be done (default is -1). str.rsplit([sep[, maxsplit]]) – Split str into a list of substrings, starting from the right. str.strip([chars]) – Return a copy of the string str with leading and trailing characters removed. The chars string specifies the set of characters to remove (default is whitespace). str.rstrip([chars]) – Return a copy of the string str with only trailing characters removed.

BUILT-IN STRING METHODS >>> "Python programming is fun!".split() ['Python', 'programming', 'is', 'fun!'] >>> " ".split('-') ['555', '867', '5309'] >>> "***Python programming is fun***".strip('*') 'Python programming is fun'

BUILT-IN STRING METHODS str.capitalize() – returns a copy of the string with the first character capitalized and the rest lowercase. str.center(width[, fillchar]) – centers the contents of the string str in field-size width, padded by fillchar (defaults to a blank space). See also str.ljust() and str.rjust(). str.count(sub[, start[, end]]) – return the number of non- overlapping occurrences of substring sub in the range [start, end]. Can use slice notation here. str.endswth(suffix[, start[, end]])– return True if the string str ends with suffix, otherwise return False. Optionally, specify a substring to test. See also str.startswith().

BUILT-IN STRING METHODS >>> "i LoVe pYtHoN".capitalize() 'I love python' >>> "centered".center(20,'*') '******centered******' >>> "mississippi".count("iss") 2 >>> "mississippi".count("iss", 4, -1) 1 >>> "mississippi".endswith("ssi") False >>> "mississippi".endswith("ssi", 0, 8) True

BUILT-IN STRING METHODS str.find(sub[, start[, end]]) – return the lowest index in the string where substring sub is found, such that sub is contained in the slice str[start:end]. Return -1 if sub is not found. See also str.rfind(). str.index(sub[, start[, end]]) – identical to find(), but raises a ValueError exception when substring sub is not found. See also str.rindex(). str.join(iterable) – return a string that is the result of concatenating all of the elements of iterable. The str object here is the delimiter between the concatenated elements. str.replace(old, new[, count]) – return a copy of the string str where all instances of the substring old are replaced by the string new (up to count number of times).

BUILT-IN STRING METHODS >>> "whenever".find("never") 3 >>> "whenever".find("what") -1 >>> "whenever".index("what") Traceback (most recent call last): File “ ”, line 1, in ValueError: substring not found >>> "-".join(['555','867','5309']) ' ' >>> " ".join(['Python', 'is', 'awesome']) 'Python is awesome' >>> "whenever".replace("ever", "ce") 'whence'

THE STRING MODULE Additional built-in string methods may be found here.here All of these built-in string methods are methods of any string object. They do not require importing any module or anything – they are part of the core of the language. There is a string module, however, which provides some additional useful string tools. It defines useful string constants, the string formatting class, and some deprecated string functions which have mostly been converted to methods of string objects.

STRING CONSTANTS >>> import string >>> string.ascii_letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> string.ascii_lowercase 'abcdefghijklmnopqrstuvwxyz' >>> string.ascii_uppercase 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> string.digits ' ' >>> string.hexdigits ' abcdefABCDEF'

STRING CONSTANTS >>> import string >>> string.lowercase #locale-dependent 'abcdefghijklmnopqrstuvwxyz' >>> string.uppercase #locale-dependent 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> string.letters # lowercase+uppercase 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> string.octdigits ' ' >>> print string.punctuation !"#$%&'()*+,-./:;

STRING CONSTANTS string.whitespace – a string containing all characters that are considered whitespace. On most systems this includes the characters space, tab, linefeed, return, formfeed, and vertical tab. string.printable – string of characters which are considered printable. This is a combination of digits, letters, punctuation, and whitespace.

STRING FORMATTING String formatting is accomplished via a built-in method of string objects. The signature is: Note that the *args argument indicates that format accepts a variable number of positional arguments, and **kwargs indicates that format accepts a variable number of keyword arguments. The string on which this method is called can contain literal text or replacement fields delimited by braces {}. Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument. A copy of the string is returned where each replacement field is replaced with the string value of the corresponding argument. str.format(*args, **kwargs)

STRING FORMATTING >>> '{0}, {1}, {2}'.format('a', 'b', 'c') 'a, b, c' >>> '{}, {}, {}'.format('a', 'b', 'c') 'a, b, c' >>> '{2}, {1}, {0}'.format('a', 'b', 'c') 'c, b, a' >>> '{2}, {1}, {0}'.format(*'abc') 'c, b, a' >>> '{0}{1}{0}'.format('abra', 'cad') 'abracadabra'

STRING FORMATTING >>> 'Coords: {lat}, {long}'.format(lat='37.24N', long=' W') 'Coords: 37.24N, W' >>> coord = {'lat': '37.24N', 'long': ' W'} >>> 'Coords: {lat}, {long}'.format(**coord) 'Coords: 37.24N, W' You can also use keyword arguments to the format function to specify the value for replacement fields

STRING FORMATTING >>> c = 2+3j >>> '{0} has real part {0.real} and imaginary part {0.imag}.'.format(c) '(2+3j) has real part 2.0 and imaginary part 3.0.' Within the replacement field, you are able to access attributes and methods of the object passed as an argument to format. Here, we pass a complex number as an argument, but we access its member attributes in the replacement field. >>> coord = (3, 5) >>> 'X: {0[0]}; Y: {0[1]}'.format(coord) 'X: 3; Y: 5'

STRING FORMATTING There are reserved sequences for specifying justification and alignment within a replacement field. >>> '{: >> '{:>30}'.format('right aligned') ' right aligned' >>> '{:^30}'.format('centered') ' centered ' >>> '{:*^30}'.format('centered') # use '*' as a fill char '***********centered***********'

STRING FORMATTING There are a number of options for formatting floating-point numbers. >>> '{:+f}; {:+f}'.format(3.14, -3.14) # show sign always ' ; ' >>> '{: f}; {: f}'.format(3.14, -3.14) # show space for positive ' ; ' >>> '{:-f}; {:-f}'.format(3.14, -3.14) # show only minus ' ; ' >>> '{:.3f}'.format( ) # limit to three dec places '3.142'

STRING FORMATTING There are still quite a few more formatting specifiers that we haven’t covered. A list of them is available here.here We’ll now turn our attention back to functions and begin OOP in Python.