ITE-EDOOP Module 8 Strings PDF

Summary

This document is a module on strings in the Python programming language. It covers the creation and manipulation of strings, along with various string methods.

Full Transcript

ITE-EDOOP (Event-Driven/Object-Oriented Programming) PAMBAYANG DALUBHASAAN NG MARILAO College of Computer Studies...

ITE-EDOOP (Event-Driven/Object-Oriented Programming) PAMBAYANG DALUBHASAAN NG MARILAO College of Computer Studies I. INTRODUCTION String usage abounds in just about all types of applications. A string consists of a sequence of characters, which includes letters, numbers, punctuation marks and spaces. To represent strings, you can use a single quote, double Module 8 quotes or triple quotes. II. OBJECTIVES STRINGS 1. Access individual characters in a string and apply basic string operations 2. Search and retrieve a substring from a string 3. Use string methods to manipulate strings III. LESSON PROPER Strings String is another basic data type available in Python. They consist of one or more characters surrounded by matching quotation marks. Creating and Storing Strings str() Function Python str() function is one of the built-in functions. This function returns the string representation of the object. If there is no argument provided, it returns an empty string. The syntax for str() function is, Page 1 of 9 ITE-EDOOP (Event-Driven/Object-Oriented Programming) PAMBAYANG DALUBHASAAN NG MARILAO College of Computer Studies str(object) Basic String Operations In Python, strings can also be concatenated using the + sign, and the * operator is used to create a repeated sequence of strings. Sample Program 1: Output Sample Program 2: Output: Page 2 of 9 ITE-EDOOP (Event-Driven/Object-Oriented Programming) PAMBAYANG DALUBHASAAN NG MARILAO College of Computer Studies Methods Description string.ascii_letters It returns a random string that contains both uppercase and lowercase characters. string_ascii_uppercase It is a random string method that only returns a string in uppercase characters. string.ascii_lowercase It is a random string method that returns a string only in lowercase characters. string.digits It is a random string method that returns a string with numeric characters. string.punctuation It is a random string method that returns a string with punctuation characters. String Repetition Python cannot concatenate string values with integer values since they are of different data types. You need to convert the integer type to string type before concatenating integer and string values. in and not in Operators You can check for the presence of a string in another string using in and not in membership operators. It returns either a Boolean True or False. The in operator evaluates to True if the string value in the left operand appears in the sequence of characters of a string value in the right operand. The not-in operator evaluates to True if the string value in the left operand does not appear in the sequence of characters of the string value in the right operand. Page 3 of 9 ITE-EDOOP (Event-Driven/Object-Oriented Programming) PAMBAYANG DALUBHASAAN NG MARILAO College of Computer Studies String Comparison You can use (>, =, ==, !=) to compare two strings resulting in either Boolean True or False value. Python compares strings using ASCII value of the characters. Suppose you have string_1 as "January" and string_2 as "June". The first two characters from string_1 and string_2 (J and J) are compared. The second two characters are compared (a and u). Built-In Functions Used in Strings There are many built-in functions for which a string can be passed as an argument. Page 4 of 9 ITE-EDOOP (Event-Driven/Object-Oriented Programming) PAMBAYANG DALUBHASAAN NG MARILAO College of Computer Studies Source Code Output Accessing Characters in String by Index Number Each character in the string occupies a position in the string. Each of the string’s character corresponds to an index number. The first character is at index 0; the next character is at index 1, and so on. The length of a string is the number of characters in it. You can access each character in a string using a subscript operator i.e., a square bracket. Square brackets are used to perform indexing in a string to get the value at a specific index or position. This is also called as subscript operator. The syntax for accessing an individual character in a string is string_name[index] Where the index is usually in the range of 0 to one less than the length of the string. The value of the index should always be an integer and indicates the character to be accessed. Source Code Ouput You can also access individual characters in a string using negative indexing. If you have a long string and want to access end characters in the string, then you can count backward from the end of the string starting from an index number of −1. String Slicing and Joining The "slice" syntax is a handy way to refer to sub-parts of a sequence of characters within an original string. The syntax for string slicing is String_name[start:end[:step]] Colon is used to specifying range value. With string slicing, you can access a sequence of characters by specifying a range of index numbers separated by a colon. String slicing returns a sequence of characters beginning at the start and extending up to but not including the end. The start and end indexing values have to be integers. Page 5 of 9 ITE-EDOOP (Event-Driven/Object-Oriented Programming) PAMBAYANG DALUBHASAAN NG MARILAO College of Computer Studies String slicing can be done using either positive or negative indexing. Source Code Output Joining String Using join() Method Strings can be joined with the join() string. The join() method provides a flexible way to concatenate strings. The syntax of join() method is string_name.join(sequence) sequence − This is a sequence of the elements to be joined. This method returns a string, which is the concatenation of the strings in the sequence seq. The separator between elements is the string providing this method. Source Code Output Page 6 of 9 ITE-EDOOP (Event-Driven/Object-Oriented Programming) PAMBAYANG DALUBHASAAN NG MARILAO College of Computer Studies Page 7 of 9 ITE-EDOOP (Event-Driven/Object-Oriented Programming) PAMBAYANG DALUBHASAAN NG MARILAO College of Computer Studies IV. PRACTICE EXERCISES/ACTIVITIES 1. Write a Python program that prompts the user to input a string. Convert the string to: o All characters to uppercase characters o Lowercase to uppercase characters and uppercase to lowercase characters o Title-cased string format Page 8 of 9 ITE-EDOOP (Event-Driven/Object-Oriented Programming) PAMBAYANG DALUBHASAAN NG MARILAO College of Computer Studies o Identify if the first character is uppercase o Count the number of vowels and consonants in the string 2. Write a Python program that prompts the user to input a string. Use the string functions to: o Return the length of the string o Count the letters, digits, and special symbols in the string o Return the reverse value of the string o Identity if the last character is lowercase o Identify if the input string contains alphanumeric characters V. ADDITIONAL RESOURCES https://www.tutorialspoint.com/python/ VI. ASSESSMENT The link for the assessment will be provided in the LMS or will be sent via Outlook mail. VII. REFERENCES Gowrishankar, S. and Veena A. 2019. Introduction to Python Programming. CRC Press. https://pynative.com Page 9 of 9

Use Quizgecko on...
Browser
Browser