Download presentation
Presentation is loading. Please wait.
1
Python Strings
2
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
3
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 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 character
4
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]
5
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
6
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]
7
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
8
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
9
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: Write some code to remove newline characters from the code Print the result in a suitable format
10
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
11
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.