Introduction to Python

Slides:



Advertisements
Similar presentations
Mathematical Functions, Strings, and Objects. Introduction ■ To solve mathematics problems by using the functions in the math module (§3.2). ■ To represent.
Advertisements

Introduction to Python
Characters and Strings. Characters In Java, a char is a primitive type that can hold one single character A character can be: –A letter or digit –A punctuation.
Chapter 2: Introduction to C++.
Guide to Programming with Python Chapter Two Basic data types, Variables, and Simple I/O: The Useless Trivia Program.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CS0004: Introduction to Programming Variables – Strings.
Constants in C A Presentation On Department of Computer & Information Technology, M.S.P.V.L. Polytechnic College, Pavoorchatram.
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
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Strings CS303E: Elements of Computers and Programming.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
12/9/2010 Course A201: Introduction to Programming.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Variables, Expressions and Statements
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Chapter 5 Strings CSC1310 Fall Strings Stringordered storesrepresents String is an ordered collection of characters that stores and represents text-based.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 3 Mathematical Functions, Strings, and Objects.
Chapter 2 Variables.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Introduction to Strings CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Strings See Chapter 2 u Review constants u Strings, concatenation and repetition 1.
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Strings Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
12 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
17-Mar-16 Characters and Strings. 2 Characters In Java, a char is a primitive type that can hold one single character A character can be: A letter or.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Mr. Crone.  Use print() to print to the terminal window  print() is equivalent to println() in Java Example) print(“Hello1”) print(“Hello2”) # Prints:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Introduction to Programming
28 Formatted Output.
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
Chapter 2 Variables.
Literals and Variables Chapter 2, Sections
Topics Designing a Program Input, Processing, and Output
Chapter 6 JavaScript: Introduction to Scripting
Chapter 2, Part I Introduction to C Programming
CSC 131: Introduction to Computer Science
Variables, Expressions, and IO
Introduction to Programming
Topics Introduction to File Input and Output
Strings Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Introduction to Programming
Introduction to Primitive Data types
Introduction to Programming
CEV208 Computer Programming
Chapter 2 Create a Chapter 2 Workspace Create a Project called Notes
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Variables, Data Types & Math
Chapter 3 Mathematical Functions, Strings, and Objects
Chapter 2: Introduction to C++.
CHAPTER 3: String And Numeric Data In Python
Introduction to Java Applications
CS1110 Today: collections.
Topics Designing a Program Input, Processing, and Output
Nate Brunelle Today: Strings, Type Casting
Topics Designing a Program Input, Processing, and Output
Introduction to Programming
Python Strings.
Chapter 2 Variables.
Topics Introduction to File Input and Output
Introduction to Primitive Data types
Introduction to Python
Introduction to Python
Presentation transcript:

Introduction to Python Strings

Topics Strings Concatenation Indexing and Slicing f-Strings Escape Sequences Multiline Strings

String Strings in Python are created with single or double quotes. In [17]: message = ‘what do you like?’ response = ‘spam' In [18]: len(response) Out [18]: 4 In [19]: # Make uppercase. See also str.lower() response.upper() Out [19]: 'SPAM’

String Concatenation In [21]: # concatenation with + message + response Out [21]: ‘what do you like?spam’ In [22]: # multiplication is multiple concatenation 5 * response Out [22]: ‘spamspamspamspamspam'

String Indexing In [23]: message = "what do you like?" In [24]: message[0] Out [24]: 'w' In [24]: # negative indices wraps around the end message[-1] # last character Out [24]: ‘?'

String Indexing and Slicing In [23]: message = "what do you like?" In [24]: # Access individual characters (zero-based indexing) message[0] Out [24]: ‘w' In [25]: message[0:4] # up to but not including index 4 Out [25]: ‘what’ In [25]: message[0:7:2] # step size of 2 Out [25]: ‘wa o'

String Indexing and Slicing In [26]: message = ”python" In [26]: # default start index is 0 message[:4] Out [26]: ‘pyth' In [27]: # default end index is length of string message[4:] Out [27]: ‘on’ In [28]: message[:] # default 0 to end of string Out [28]: ‘python’

String Indexing and Slicing In [26]: message = ”python" In [24]: # negative indices wraps around the end message[-1] # last character Out [24]: ‘n' In [25]: # all except the last character message[:-1] Out [25]: ‘pytho’ In [25]: # negative step size traverses backwards message[::-1] Out [25]: ‘nohtyp’

f-Strings f-Strings is the new way to format strings in Python. (v 3.6) In [26]: name = “Mike” gpa = 3.2 f_str = f“I am {name} with a {gpa} gpa.” print(f_str) Out [26]: ‘I am Mike with a 3.2 gpa.'

f-Strings Precision In [26]: import math x = math.pi print(f“{x}”) print(f“{x:.2f}”) print(f“{x:.3f}”) 3.141592653589793 3.14 3.142

str() The function str() can be construct string objects from integer or float literals. In [1]: y = str(2)    # y will be '2'     z = str(3.0)  # z will be '3.0'

Special Characters It is not valid syntax to have a single quote inside of a single quoted string. In [1]: ‘that’s not legal’                          File "<ipython-input-7-2762381d46b7>", line 1     'that's not legal'           ^ SyntaxError: invalid syntax Instead, we can use double quotes outside the string. In [2]: print(“It’s legal to do this.”, ‘And he said, “This is ok.”’)     It’s legal to do this. And he said, “this is ok.”                    

Escape Sequence Escape sequence is a special sequence of characters used to represent certain special characters in a string. \n new line character \” double quote \’ single quote \\ backslash character \t tab

Escape Sequence What is the output? How many ‘lines’ are shown “here”? In [11]: print(“How \tmany \‘lines\’\n are\n shown\n \“here\”?”)                                             How many ‘lines’ are shown “here”?

Multiline String To span multiple lines, put three single quotes or three double quotes around the string instead of one. The string can then span as many lines as you want: In [1]: '''three      lines       of output’’’                                                 Out[1]: 'three\nlines \nof output’ Notice that the string Python creates contains a \n sequence everywhere our input started a new line. Each newline is a character in the string. Multiline strings are often used in documentation strings as we will see later.

References Vanderplas, Jake, A Whirlwind Tour of Python, O’reilly Media. This book is completely free and can be downloaded online at O’reilly’s site. 2) Paul Gries, Jennifer Campbell, Jason Montojo, Practical Programming, The Pragmatic Bookself. 2017.