Python Strings. String  A String is a sequence of characters  Access characters one at a time with a bracket operator and an offset index >>> fruit.

Slides:



Advertisements
Similar presentations
Container Types in Python
Advertisements

Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
Java Programming Strings Chapter 7.
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.
STRINGS IN PYTHON. Where have we seen strings before? #string type variables s = “hello” #obtaining keyboard input from the user choice = input(“Please.
Strings. A string is a series of characters Characters can be referenced by using brackets The first character is at position 0 mystring = “the” letter.
Computing with Strings CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
Guide to Programming with Python
Programming for Linguists An Introduction to Python 17/11/2011.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Python for Informatics: Exploring Information
Python Programming Chapter 7: Strings Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Introduction to Computing Using Python Imperative Programming  what is a Python program?  print() statement  input() statement  type conversion statements.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Programming in Python Part III Dr. Fatma Cemile Serçe Atılım University
Strings CS303E: Elements of Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Lecture 19 - More on Lists, Slicing Lists, List Functions COMPSCI 101 Principles of Programming.
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:
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
More Strings CS303E: Elements of Computers and Programming.
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
SEQUENCES:STRINGS,LISTS AND TUPLES. SEQUENCES Are items that are ordered sequentially and accessible via index offsets into its set of elements. Examples:
Python Files and Lists. Files  Chapter 9 actually introduces you to opening up files for reading  Chapter 14 has more on file I/O  Python can read.
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.
Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from.
Strings … operators Up to now, strings were limited to input and output and rarely used as a variable. A string is a sequence of characters or a sequence.
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.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
Unit 4 – Chapter 4 Strings and Tuples. len() Function You can pass any sequence you want to the len() function and it will return the length of the sequence.
Python Basics.
String and Lists Dr. José M. Reyes Álamo.
CSc 120 Introduction to Computer Programing II Adapted from slides by
Module 4 String and list – String traversal and comparison with examples, List operation with example, Tuples and Dictionaries-Operations and examples.
Tuples and Lists.
Python - Lists.
Strings Part 1 Taken from notes by Dr. Neil Moore
CHAPTER 7 STRINGS.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Strings
Imperative Programming
Introduction to Strings
Decision Structures, String Comparison, Nested Structures
Python - Strings.
Data types Numeric types Sequence types float int bool list str
Strings.
8 – Lists and tuples John R. Woodward.
Chapter 8 More on Strings and Special Methods
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Presented by Mirza Elahi 10/29/2018
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Basic String Operations
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Strings
For loop Using lists.
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Imperative Programming
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Presentation transcript:

Python Strings

String  A String is a sequence of characters  Access characters one at a time with a bracket operator and an offset index >>> fruit = ‘banana’ >>>letter = fruit[1] >>>print (letter) a  Index must be an integer >>>letter = fruit[1.5] TypeError: string indices must be integers, not float

len  Built-in function returns number of characters in a string >>>fruit = ‘banana’ >>> len(fruit) 6  Getting the last character in a string the wrong way >>> length = len(fruit) >>> last = fruit[length] IndexError: string index of range  Getting the last character in a string the right way >>> last= len(fruit-1) >>> print (last) a

Traversal index = 0 while index < len(fruit): letter = fruit[index] print (letter) index = index + 1 for char in fruit print (char)

String slices  A slice is a segment of a string ◦Selecting a slice is similar to selecting a char >>>s = ‘Monty Python’ >>> print (s[0:5]) #returns the 0 to the 4 th char Monty  Can omit the first index >>>s = ‘Monty Python’ >>> print (s[:5]) #returns the 0 to the 4 th char Monty  Can omit the second index >>>s = ‘Monty Python’ >>> print (s[6:]) #returns the 6th to the last char Python  If the first index is >= second, returns the empty string

Strings are immutable  Don’t use [] on left side of assignment operator to attempt to change a char in the string >>> greeting = ‘Helli’ >>> greeting[4] = ‘o’ TypeError: ‘str’ object does not support item assignment  You cannot change an existing immutable object. ◦Create a new one (and reassign) >>> greeting = ‘Helli’ >>> greeting2 = greeting[0:4]+’o’ >>> print (greeting2) Hello

String Methods  upper(word) >>>word = ‘flower’ >>>upword = word.upper() >>>print (upword) FLOWER  find(char) >>>word = ‘flower’ >>>index = word.find(‘o’) >>>print (index) 2  find(string) >>>word = ‘flower’ >>>index = word.find(‘ow’) >>>print (index) 2  find(string, start, end)

String operators  in (s1, s2) ◦Takes two strings and returns True if the first appears in the second >>> ‘a’ in ‘banana’ True >>> ‘seed’ in ‘banana’ False  Relational operators work on strings >>> if word == ‘banana’ print (‘All right, bananas.’)

Problem: Palindrome # Program to check if a string # is palindrome or not # take input from the user my_str = input("Enter a string: ") # make it suitable for caseless comparison my_str = my_str.casefold() # reverse the string rev_str = reversed(my_str) # check if the string is equal to its reverse if list(my_str) == list(rev_str): print("It is palindrome") else: print("It is not palindrome")