Python Strings

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Built in functions for strings?

Various built-in functions that work with sequence work with strings as well. Some of the commonly used ones are enumerate() and len(). The enumerate() function returns an enumerate object. It contains the index and value of all the items in the string as pairs. This can be useful for iteration. Similarly, len() returns the length (number of characters) of the string.

How to access characters in a string?

We can access individual characters using indexing and a range of characters using slicing. Index starts from 0. Trying to access a character out of index range will raise an IndexError. The index must be an integer. We can't use floats or other types, this will result into TypeError. Python allows negative indexing for its sequences. The index of -1 refers to the last item, -2 to the second last item and so on. We can access a range of items in a string by using the slicing operator :(colon)

Iterating Through a string?

We can iterate through a string using a for loop. Here is an example to count the number of 'l's in a string.

String Membership Test?

We can test if a substring exists within a string or not, using the keyword in.

How to delete a string?

We cannot delete or remove characters from a string. But deleting the string entirely is possible using the del keyword.

How does slicing work?

start: The starting index of the substring. The character at this index is included in the substring. If start is not included, it is assumed to equal to 0. end: The terminating index of the substring. The character at this index is NOT included in the substring. If end is not included, or if the specified value exceeds the string length, it is assumed to be equal to the length of the string by default. step: Every 'step' character after the current character to be included. The default value is 1. If the step value is omitted, it is assumed to equal to 1.

What is Regular Experssion in Python?

A regular expression (shortened as regex or regexp; also referred to as rational expression) is a sequence of characters that define a search pattern. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation. It is a technique developed in theoretical computer science and formal language theory.

What is String in Python?

A string is a sequence of characters. A character is simply a symbol. For example, the English language has 26 characters. Computers do not deal with characters, they deal with numbers (binary). Even though you may see characters on your screen, internally it is stored and manipulated as a combination of 0s and 1s. This conversion of character to a number is called encoding, and the reverse process is decoding. ASCII and Unicode are some of the popular encodings used. In Python, a string is a sequence of Unicode characters. Unicode was introduced to include every character in all languages and bring uniformity in encoding. You can learn about Unicode from Python Unicode.

Escape Sequence?

An escape sequence starts with a backslash and is interpreted differently. If we use a single quote to represent a string, all the single quotes inside the string must be escaped.

Slicing....

If we want to access a range, we need the index that will slice the portion from the string.

Concatenation of Two or More Strings?

Joining two or more strings into a single one is called concatenation. The + operator does this in Python. Simply writing two string literals together also concatenates them. The * operator can be used to repeat the string for a given number of times.

The module re.

Some methods in the module re. findall: Returns a list containing all matches. search: Returns a Match object if there is a match anywhere in the string. split: Returns a list where the string has been split at each match. sub: Replaces one or many matches with a string. Sets usually between brackets to include or execlude letters or digits.

Common Python String Methods other than format()?

Some of the commonly used methods are lower(), upper(), join(), split(), find(), replace() etc.

Raw String to ignore escape sequence?

Sometimes we may wish to ignore the escape sequences inside a string. To do this we can place r or R in front of the string. This will imply that it is a raw string and any escape sequence inside it will be ignored.

How to change a string?

Strings are immutable. This means that elements of a string cannot be changed once they have been assigned. We can simply reassign different strings to the same name.

The format() Method for Formatting Strings

The format() method that is available with the string object is very versatile and powerful in formatting strings. Format strings contain curly braces {} as placeholders or replacement fields which get replaced. We can use positional arguments or keyword arguments to specify the order.


Set pelajaran terkait

Med-Surg Hemotopietic and Lymphatic system

View Set

Cisco WLC Configuration & Wireless Management Access

View Set

Physical and Chemical Weathering

View Set