Introduction to Python Strings Strings v A Python

  • Slides: 10
Download presentation
Introduction to Python Strings

Introduction to Python Strings

Strings v A Python strings is a datatype be it a letter, number, punctuation

Strings v A Python strings is a datatype be it a letter, number, punctuation Introduction to Python or any other keyboard character v To initialise enclose the text with a single or double quote v To include a quote in the string use the backward slash v 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? v Slicing means taking part of a string

Slicing Strings – What is it? v Slicing means taking part of a string as in cutting it up Introduction to Python v It is often useful to take part of a string or analyse it or use it is another part of the code. v 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 v Strings have indices just like lists: Introduction to

Slicing Strings – How To v Strings have indices just like lists: Introduction to Python q Forward index starts at 0 from the LHS q Reverse index starts at -1 from the RHS v To specify a slice you need to q give the START position from the left hand side q give the END position from the left hand side (characters to the LHS of this number are included in the slice q Separate the two values with a colon v Example: new. String = my. String [1: 3]

Slicing Strings – Some Rules v If you don’t specify a START it defaults

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

Slicing Strings – Quickstart 0 1 M 2 i -9 3 n -8 4

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

Useful string Functions and Methods Name Purpose Example code * Repeat a string print(f"{'%’*4})

Useful string Functions and Methods Name Purpose Example code * Repeat a string print(f"{'%’*4}) + Concatenates (joins) two strings together full. Title = string. Title + string. Subtitle capitalize Converts the first letter of the string to uppercase capital. Title = full. Title. capitalize() count Counts the number of times a substring occurs char. Count = full. Title. count("a") Finds the first position of a string in within another char. Pos = string. Title. find("a") len The len function to calculates how many characters in the string including spaces string. Len = len(my. String) lower Converts all alphabetic characters to lowercase lower. Title = full. Title. lower() replace Replace all occurrences of one character with another replace. Title = full. Title. replace("a", "@") strip Removes whitespace (tabs and space) from the beginning and end of a string strip. Title = full. Title. strip() title Converts the first letter of every word to uppercase title. Title = full. Title. title() upper Converts all alphabetic characters to uppercase (capitals) upper. Title = full. Title. upper() Introduction to Python

Exercises 1 1. (save as String Length) Counting characters in a string a) Write

Exercises 1 1. (save as String Length) Counting characters in a string a) Write a program to calculate the number of characters in the string: Minecraft is awesome b) Print the string Introduction to Python c) Print the result of the calculation. 2. (save as Substrings) Using the string from the previous exercise a) Create and print substrings comprising first and last letters a) Create and print a word comprising the first two letters and the last two letters b) Create and print a substring and its length, comprising the letters after the letter t and before the second letter a Note: To complete this task, you are expected to use a language feature to find the position of the characters 3. a) b) c) d) 4. (save as Change Case) Changing the case of a sentence 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 Upper Case Test) a) Write a program to that allows the user to enter a sentence b) Change all the characters to uppercase if there at least two uppercase letters in the first 4 5. Replacing letters in a string (save as Replace Char) a) Write a program that for a given string, all occurrences of its first letter have been changed to '$', except the first character itself. b) Test your program using the word agama (a species of lizard) c) Print the original string and the new string

Exercises 2 1. Set string. One=“Skeleton” string. Two=“Horseman” (save as Swaps) a) Make two

Exercises 2 1. Set string. One=“Skeleton” string. Two=“Horseman” (save as Swaps) a) Make two new strings by swapping the first three letters of the string. One with the last three letters of the sti string. Two Introduction to Python b) Make a single string from the two new strings c) Print: The strings in their original order, The two new strings and the combined string 2. Are you a Mr? (save as Starts With) a) Allow the user to input a string b) Check if the first two characters are Mr and only one word follows the title eg (Mr Jones). c) If the above conditions are met print “Pleased to meet you Mr xxx” where xxx represents the name the user entered d) Otherwise print “Your title is not recognized” 3. String reverse (save as String Reverse) a) Allow the user to input a string b) If the length a multiple of 4 reverse the string c) Otherwise give a suitable error message 4. Remove Newline characters from a sentence a) Download the starter code from this link: https: //bit. ly/2 Goph 4 P b) Write some code to remove newline characters from the code c) Print the result in a suitable format (save as No Newline)

Exercises 3 1. Write a program that allows the user to enter a string

Exercises 3 1. Write a program that allows the user to enter a string then: a) If length of the string is less than 3 give an error message and ask for the string to be re-input a) If the given string already ends with 'ing'Introduction then replace ing with instead to 'ly' Python b) add 'ing' at the end of a given string c) Print the input string and the new string Sample String : 'abc' Expected Result : 'abcing’ 2. Write a program that allows the user to enter a sentence then: (save as ingly) (save as Longest Word) a) Count the number of characters in each word. b) word is defined as a series of characters separated by a space c) Print: the sentence, the longest word, the number of characters in the longest word 3. Write a program that allows the user to enter a string then: a) b) c) d) 4. Write a program that allows the user to enter a string then: a) b) c) d) e) (save as Copies) If the length of the string is less than 3 , 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. Print the sentence, the longest word and the number of characters in the word 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: hehe (save as Last Four)