Strings - Middle Georgia State University

Summary

This document is a lecture presentation from the School of Computing at Middle Georgia State University, covering essential Python programming concepts related to strings. Topics include sequences, characters, string manipulation, operators, and object-oriented programming techniques.

Full Transcript

Strings School of Computing Content Introduction to Strings The Concept of Sequences Strings as Sequences of Characters Accessing Characters in Strings String Literals and Concatenation Joining and Splitting Strings Escape Sequences in Strings Object-Oriented Program...

Strings School of Computing Content Introduction to Strings The Concept of Sequences Strings as Sequences of Characters Accessing Characters in Strings String Literals and Concatenation Joining and Splitting Strings Escape Sequences in Strings Object-Oriented Programming with Strings Immutability of Strings Indexing and Slicing Strings Pattern Matching with Regular Expressions The split Method Introduction to Strings Sequences and Real-world Applications – Grasp the concept of sequences – Recognize their importance in real-world scenarios Character Manipulation – Use indexing and slicing for character operations String Operations – Master concatenation, joining, and splitting – Implement escape sequences in strings String Methods – Modify and process strings with built-in methods Pattern Matching String Formatting The Concept of Sequences Defining Sequences – An ordered list of items, integral to daily activities – Examples include car positions, to-do lists, and alphabetical order Real-World Sequence Examples – Stock prices on a ticker – Chronological exam scores – Scientific measurements over time – Alphabetized names – Text message series Importance in Software – Sequences represent related data for processing – Reflect real-world events or processes Strings as Sequences of Characters Strings as Character Sequences – Each character in a Python string is also a string Interactive Shell Example – Typing 'type('h')' selects and prints the data type of 'h' String Data Type Confirmation – The result shows that each character in a string is also considered a string in Python. String Processing Techniques – Accessing and modifying string elements similar to other sequences – Unique formatting techniques specific to strings Accessing Characters in Strings Working with Strings in Programs – Handling entire strings or specific characters – Example: Printing names or accessing to-do list items Processing Strings with Operators – Using sequence literals or variables – Example: myname accesses the first character Operators in Python – Symbols performing operations on operands – Example: Adding employment and retirement income Calling Methods on String Objects – Using built-in methods for string processing – Example: myname.upper() converts to uppercase myname = "John" print(myname) # Output: 'J' employment_income = 50000 retirement_income = 150000 print(employment_income + retirement_income) # Output: 200000 myname = "John" print(myname.upper()) # Output: 'JOHN' String Literals and Concatenation Definition of String Literals – Actual string content in source code – Visible and initially assigned or directly used Concatenation Example – Using the + operator to merge strings – Python interprets + as concatenation between strings print("Take everything " + "literally") Joining and Splitting Strings Joining Strings with 'join' Method – Concatenates strings with a specified separator – Example: ['192', '168', '1', '1'] becomes '192.168.1.1' Splitting Strings with 'split' Method – Divides a string into a list based on a separator – Example: '192.168.1.1' becomes ['192', '168', '1', '1'] >>>".".join(["192", "168", "1", "1"]) '192.168.1.1' >>>'192.168.1.1'.split(".") ['192', '168', '1', '1'] Escape Sequences in Strings Definition of Escape Sequences – Symbols representing special characters within a string Functionality – Used for special content like new lines or tabs Examples – New line symbol (\n) – Tab space symbol (\t) Usage – Printing invisible string content visibly print("I need many lines to express my\n\n\ nbrilliance") print("Tabs...\taren’t enough to do it") Object-Oriented Programming with Strings Object-Oriented Programming Concepts – Objects contain data and methods to work with data Strings in Python are 'str' objects with various capabilities – String Manipulation Techniques – Storing strings in variables Indexing strings to access specific parts Using string methods like 'join' and 'lower' (Example) # Store two strings in variables myname = "Monty" mylastname = "Python" # Include the strings in expressions to use their values print("Yeah, I’m " + myname + " " + mylastname + ". Who’s asking?") # Index the strings to access particular parts of them print("Shorten my name to " + myname + mylastname + " if you want, but that’s rude") print("My username? " + myname + mylastname + ". Why are you asking for this??") # join method joins the two strings 'around' the contents of the string object #it is called from print("I was told to add strange characters to my password, so I made it " + "✩!@".join([myname, mylastname]) + ". Arggh.. I just said that out loud, didn’t I...") # Call a function from the variable to process it, then print it print(myname.lower()) Output: Yeah, I’m Monty Python. Who’s asking? Shorten my name to MP if you want, but that’s rude My username? MPython. Why are you asking for this?? I was told to add strange characters to my password, so I made it Monty✩!@Python. Arggh.. I just said that out loud, didn’t I... monty Immutability of Strings Definition of String Immutability – Strings cannot be altered once created – Direct character changes result in errors Modifying Strings – Creation of new strings for changes – Combining characters for new string versions Variable Reassignment – Reassigning references to new strings – Original strings remain unchanged in memory >>>greeting = "Hello" >>>greeting = ‘Y’ >>> TypeError: 'str' object does not support item assignment Indexing and Slicing Strings Indexing: Selecting Specific Characters – Starts at position 0, use [ ] with an integer – mystring retrieves the first character Slicing: Selecting a Range of Characters – Use a colon : to specify start and end indexes – mystring[1:3] gets characters from index 1 to 2 Combining Characters: Creating New Strings – Combine characters from different positions – Form new strings with multiple indexes  Indexing: Picking a Single Character You can select a single character from a string by its position. Positions start at 0, not 1. mystring = 'Monty' print(mystring) # Output: 'M' print(mystring) # Output: 'o' print(mystring) # Output: 'n'  Combining Characters: Using Multiple Indexes You can combine characters from different positions to create new strings. print(mystring + mystring) # Output: 'My' print(mystring + 'onda' + mystring) # Output: 'Monday'  Slicing: Picking a Substring To pick a range of characters, specify a starting index and an ending index. The ending index is not included in the result. print(mystring[0:3]) # Output: 'Mon' print(mystring[3:]) # Output: 'ty' print(mystring[3:5]) # Output: 'ty' Pattern Matching with Regular Expressions Introduction to Regular Expressions – Powerful string matching patterns in Python – Implemented via the built-in re module Python Interactive Mode Example – Illustration of re.match method – Returns a Match object for found patterns Special Characters in Patterns –.* for any character(s), repeated any number of times –. for any single character Practical Applications – Text pattern searching – Input data validation >>>import re # Get help information about re.match >>>help(re.match) # Use re.match to find a match for the pattern ".*bob.*" in the string "bob and alice" >>>match = re.match(".*bob.*", "bob and alice") >>>print(match) Output will be: The split Method Basic Usage of split – Divides string based on delimiter – Default delimiter is whitespace – Optional maxsplit argument Splitting by Specific Characters – Can use characters or substrings as delimiters – Example: Splitting a CSV line by commas Using maxsplit Argument – Limits number of splits performed – Example: Splitting a list by first two commas Splitting with Regular Expressions Splitting by Newlines Basic Usage of split The split method takes a string and splits it into parts based on a specified delimiter. The syntax is as follows: string.split(delimiter, maxsplit)  delimiter: The character or substring on which to split the string. If not specified, the default delimiter is any whitespace.  maxsplit: An optional argument that specifies the maximum number of splits. If not specified, the string is split at every occurrence of the delimiter.  Splitting by Whitespace (Default Behavior) If no delimiter is provided, the split method splits the string at any whitespace (spaces, tabs, or newlines). text = "This is a sample string" words = text.split() print(words) # Output: ['This', 'is', 'a', 'sample', 'string']  Splitting by a Specific Character You can split a string using a specific character as the delimiter. For example, splitting by a comma: csv_line = "apple,banana,cherry" fruits = csv_line.split(',') print(fruits) # Output: ['apple', 'banana', 'cherry']  Splitting by a Substring The delimiter can also be a substring. text = "Hello---world---Python" parts = text.split('---') print(parts) # Output: ['Hello', 'world', 'Python']  Using maxsplit Argument The maxsplit argument specifies the maximum number of splits to be done. If provided, the string is split at the first maxsplit occurrences of the delimiter. text = "one, two, three, four, five" parts = text.split(',', 2) print(parts) # Output: ['one', ' two', ' three, four, five']  Splitting by Multiple Characters (Regular Expressions) To split by multiple different characters, you can use the re module's split function. Here’s how you can split by both comma and space: import re text = "apple, banana, cherry; orange" parts = re.split('[,; ]+', text) print(parts) # Output: ['apple', 'banana', 'cherry', 'orange'] Formatting Strings in Python Essentials of String Formatting – Improves data readability and presentation – Utilizes format() method for variable insertion Basic Formatting Techniques – Using curly braces as placeholders – Example provided for clarity Advanced Formatting Options – Field width, alignment, and right-justification – Formatting for numerical data representation Practical Application – Change Counter program calculates total coin value – Output demonstrates formatted string usage Basic Formatting Using format() The format() method in Python allows you to insert variables into a string in specific places, which makes it easy to create well-formatted strings. username = "Monty" password = "123456" print('You entered "{}" "{}"'.format(username, password)) Output will be: You entered "Monty" "123456" Example 1: Change Counter The Output of Running the Program Change Counter # Function defined to do the work def main(): print("Change Counter\n") Please enter the count of each coin type. Quarters: 1 print("Please enter the count of each coin type.") Dimes: 1 quarters = eval(input("Quarters: ")) Nickels: 1 dimes = eval(input("Dimes: ")) Pennies: 1 nickels = eval(input("Nickels: ")) The total value of your change is $0.41 pennies = eval(input("Pennies: ")) total = quarters * 25 + dimes * 10 + nickels * 5 + pennies # Each {} contains an index number and symbols control appearance. # Total is divided by 100 with integer division for the first parameter (dollars) # and modulus with 100 for the second parameter (cents) # See https://docs.python.org/3.6/library/stdtypes.html#str.format print("The total value of your change is ${0}.{1:02}".format(total // 100, total % 100)) Encoding and Decoding Strings String Encoding Basics – Conversion of characters to numeric Unicode representation Encoding and Decoding Characters in – Use of 'ord' function for encoding Python String Decoding Process Numeric Characte Decode – Reverting numeric codes back to (Unicode Encoded r d ) characters – Application of 'chr' function for M 77 ord('M') chr(77) decoding o 111 ord('o') chr(111) Python String Manipulation n 110 ord('n') chr(110) – Indexing and concatenation techniques t 116 ord('t') chr(116) – Building strings with specific y 121 ord('y') chr(121) characters Example 2: Encoding # Sequences: encoding # A string message to encode. mymessage = 'Monty' # Try out indexing and concatenation print(mymessage + mymessage + mymessage) # Encode: in this case we can encode and decode a character from a string. for char in mymessage: print('Numeric version:', ord(char)) print('Recover the original:', chr(ord(char))) Summary of String Concepts Ordered Sequences and Characters – Strings as sequences of characters, each character a string itself. – Indexing to access individual characters, e.g., 'h' from 'help'. String Processing and Operators – Whole entity processing or specific character access. – Concatenation with '+', e.g., 'Hello World'. Transformation and Literals – Methods like.upper() transform strings, e.g., 'HELLO'. – String literals for initial assignments. Joining and Splitting Special Characters and Immutability Advanced Operations