Python Strings.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

Computer Science & Engineering 2111 Text Functions 1CSE 2111 Lecture-Text Functions.
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.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
UNIX Filters.
Chapter 5 new The Do…Loop Statement
Noadswood Science,  To understand what strings are and how decisions are made Thursday, September 17, 2015.
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.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
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.
Strings CS303E: Elements of Computers and Programming.
Manipulating Text In today’s lesson we will look at: why we might want to pick out parts of text strings some BASIC functions that can be used to chop.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Instructor: Craig Duckett Lecture 08: Thursday, October 22 nd, 2015 Patterns, Order of Evaluation, Concatenation, Substrings, Trim, Position 1 BIT275:
Chapter 5 Strings CSC1310 Fall Strings Stringordered storesrepresents String is an ordered collection of characters that stores and represents text-based.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
More Strings CS303E: Elements of Computers and Programming.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Variables and Strings. Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 2 Karsten Hokamp, PhD Genetics TCD, 17/11/2015.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
INLS 560 – S TRINGS Instructor: Jason Carter. T YPES int list string.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
02/03/ Strings Left, Right and Trim. 202/03/2016 Learning Objectives Explain what the Left, Right and Trim functions do.
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.
13/06/ Strings Left, Right and Trim. 213/06/2016 Learning Objectives Explain what the Left, Right and Trim functions do.
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.
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
String and Lists Dr. José M. Reyes Álamo.
String Methods Programming Guides.
Topics Designing a Program Input, Processing, and Output
String Processing Upsorn Praphamontripong CS 1110
Variables, Expressions, and IO
Strings Part 1 Taken from notes by Dr. Neil Moore
Chapter 8 - Characters and Strings
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
IDENTIFIERS CSC 111.
CS 106A, Lecture 9 Problem-Solving with Strings
String Manipulation Part 2
Introduction to C++ Programming
Manipulating Text In today’s lesson we will look at:
String Manipulation Chapter 7 Attaway MATLAB 4E.
CSC 221: Introduction to Programming Fall 2018
String and Lists Dr. José M. Reyes Álamo.
Basic String Operations
CS 1111 Introduction to Programming Spring 2019
Topics Designing a Program Input, Processing, and Output
String manipulation string.h library
Topics Basic String Operations String Slicing
Topics Designing a Program Input, Processing, and Output
String Processing 1 MIS 3406 Department of MIS Fox School of Business
Introduction to Computer Science
Python Review
Topics Basic String Operations String Slicing
Exam Prep.
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Topics Basic String Operations String Slicing
What We Want To Do User enters: Mary Smith
Presentation transcript:

Python Strings

Strings A Python strings is a datatype be it a letter, number, punctuation or any other keyboard character To initialise enclose the text with a single or double quote To include a quote in the string use the backward slash This tells the interpreter, this is an apostrophe, not an opening quote to another string, so ignore it”. In python are treated as a special type of list

Slicing Strings – What is it? Slicing means taking part of a string as in cutting it up It is often useful to take part of a string or analyse it or use it is another part of the code. Example: you have been asked to write a program to check if a string contains a valid email address. One of the checks will mean that you need to slice the string taking all the characters from the left hand part of the string up to the @ character

Slicing Strings –How to do it Strings have indices just like lists: Forward index starts at 0 from the LHS Reverse index starts at -1 from the RHS To specify a slice you need to give the START position from the left hand side give the END position from the left hand side Separate the two values with a colon Example: newString = myString [1:3]

Slicing Strings –Some Rules If you don’t specify a START it defaults to the beginning: If you don’t specify a END it defaults to the end: newString = myString [:3] This means start at position 0 end at position 3 This means start at position until the end newString = myString [3:] When SLICING, think of the index as pointing to the character to the left of the arrow 1 2 3 4 5 6 7 8 M i n e c r a f t -9 -8 -7 -6 -5 -4 -3 -2 -1 newString = myString [1:3] The content of newString would be: in

Slicing Strings – Quickstart 1 2 3 4 5 6 7 8 M i n e c r a f t -9 -8 -7 -6 -5 -4 -3 -2 -1 First Character Last Character: All characters but the first: All characters but the last: firstChar = myString [0] M firstChar = myString [-1] t inecraft slice = myString[1:] Minecraf slice = myString[:-1]

Useful string Functions and Methods Name Purpose len(myString) Returns the number of characters in a string (including spaces) + Concatenates (joins) two strings together * Repeat a string myString.find(s) Finds the first position of s in myString myString.count(s) Counts the number of times s occurs in myString myString.lower() Converts all alphabetic characters to lowercase myString.upper() Converts all alphabetic characters to uppercase (captials) myString.title() Converts the first letter of every word to uppercase myString.strip(s) Removes whitespace (tabs and space) from the beginning and end of a string myString.replace(s,t) Replace all occurrences of s with t

Exercises 1 Counting characters in a string (save as Char Count) Write a program to calculate the number of characters in the string: Minecraft is Awesome Print the string Print the result of the calculation. Using the string from the previous exercise (save as Substrings) Create and print a substring comprising first and last characters Create and print a substring comprising the first two characters and the last two characters Create and print a substring comprising the letters after the character t and before the second character a Changing the case of a sentence (save as Change Case) Write a program to that allows the user to enter a sentence Change the sentence to UPPERCASE, lowercase, Title case, Capitalise Each Word Print the original string Print the four different versions re-cased string Change a string to uppercase with a test (save as Change Case with Test) Write a program to that allows the user to enter a string Change all the characters to uppercase if there at least two uppercase characters in the first 4 5. Replacing letters in a string (save as Replace Char) Write a program that for a given string, all occurrences of its first character have been changed to '$', except the first character itself. Test your program using the word agama (a species of lizard) Print the original string and the new string

Exercises 2 Replacing letters in a string (save as Replace Char) Write a program that for a given string, all occurrences of its first character have been changed to '$', except the first character itself. Test your program using the word agama (a species of lizard) Print the original string and the new string 2. Using the two strings: Skeleton Horseman (save as Swaps) Make two new strings by swapping the first three letters of the first string with the last three letters of the second string Make a single string from the two new strings Print: The strings in their original order, The two new strings and the combined string Expected output: 3. Are you a Mr? (save as Starts With) Write a program to check whether a string input by the user starts with Mr 4. String reverse (save as String Reverse) Allow the user to input a string If it’s a multiple of 4 reverse the string Otherwise give a suitable error message 5. Remove Newline characters from a sentence (save as No Newline) Download the starter code from this link:https://bit.ly/2Goph4P Write some code to remove newline characters from the code Print the result in a suitable format

Exercises 3 Write a program that allows the user to enter a string then: If length of the string is less than 4 give an error message (save as ingly) add 'ing' at the end of a given string (length should be at least 3). If the given string already ends with 'ing' then replace ing with 'ly' instead. If the string length of the given string is less than 3, leave it unchanged.  Sample String : 'abc' Expected Result : 'abcing'  Write a program that allows the user to enter a sentence then: (save as Longest Word) Count the number of characters in each word. word is defined as a series of characters separated by a space Print: the sentence, the longest word, the number of characters in the longest word Write a program that allows the user to enter a string then: (save as Copies) If the length of the string is less than 3 , give an error message If the string length of the given string is less than 3, leave it unchanged. Print the sentence, the longest word and the number of characters in the word Write a program that allows the user to enter a string then: (save as Last Four) If the length of the string is less than 2 , give an error message Otherwise make a new string that is made from 4 copies of the last two characters Print the new string Input: the Output: hehehehe

Exercises 4 Write a program that allows the user to enter a string then: (save as abcing) If length of the string is less than 4 give an error message add 'ing' at the end of a given string (length should be at least 3). If the given string already ends with 'ing' then replace ing with 'ly' instead. If the string length of the given string is less than 3, leave it unchanged.  Sample String : 'abc' Expected Result : 'abcing'  Write a program that allows the user to enter a string then: (save as ingly) If the length of the string is less than 3 , give an error message If the string length of the given string is less than 3, leave it unchanged. Print the sentence, the longest word and the number of characters in the word 3. Write a program that allows the user to enter a string then: (save as First Three) If the length of the string is less than 3 , give an error message and print the input string Otherwise, make a new string from the first three characters if the original string Print the new string