We will cover some of the most commonly found methods -str.capitalize(): Returns a copy of the string with only the first character capitalized and all others lowercase.str.endswith(suffix[, start[, end]]): Returns True if the string ends with the specified suffix; otherwise, returns False. To look for multiple suffixes, a tuple can be used. The optional start is the index to start the search at,...
Saturday, May 28, 2022
Thursday, May 26, 2022
Sequence methods
The following methods are common to most sequence types, such as lists, tuples, sets, and strings, except where indicated:x in seq: True if an item within the sequence is equal to x; otherwise, False is returned. This also applies to a subset of a sequence, such as looking for a specific character within a string.x not in seq: True if no item within the sequence is equal to x; otherwise, False is...
Monday, May 23, 2022
Sets
Sets are unordered collections of hashable objects; in other words, each object is unique. Sets are commonly used to see if a collection of objects contains a particular item, remove duplicates from a sequence, and compute a variety of mathematical operations.Sets look like dictionaries, in that curly...
Friday, May 20, 2022
Tuple unpacking
To create a tuple, we simply create a variable and assign items to it, separated by commas. The term for this is packing a tuple, because the data is packed into the tuple, all wrapped up and ready to go. To remove items from a tuple, you simply unpack it, as shown in the following screenshot:In line...
Wednesday, May 18, 2022
Tuples
The final built-in data type is the tuple. Python tuples work exactly like Python lists except they are immutable; that is, they can't be changed in place. They are normally written inside parentheses to distinguish them from lists (which use square brackets), but as you'll see, parentheses aren't always...
Monday, May 16, 2022
Working with dictionaries
There are a large number of methods that can be used with dictionaries. We won't talk about all of them, but some of the more common ones are shown in the following screenshot:Line 36 checks to see whether a specified key exists within the dictionary.Line 37 returns all the items that exist within the...
Friday, May 13, 2022
Dictionaries
Python lists, as previously shown, are ordered collections that use a numerical offset. To select an item in a list, you need to know its position within the list. Python dictionaries are unordered collections of objects, matched to a key name; in other words, you can reference an item simply by knowing...
Thursday, May 12, 2022
Mutability
As mentioned several times, one of the special things about lists is that they are mutable; that is, they can be modified in place without creating a new object. The big concern with this is remembering that, if you do this, it can affect other references to it. However, this isn't usually a large problem;...
Tuesday, May 10, 2022
Adding list elements
Adding new items to a list is extremely easy. You simply tell the list to add them, as shown in the following screenshot. This also demonstrates how any item can be placed in a list, even disparate data types:The append() method simply adds a single item to the end of a list; it's different from...
Monday, May 9, 2022
Lists
Lists in Python are one of the most versatile collection object types available. The other workhorses are dictionaries and tuples, but they are really more like variations of lists. Python lists do the work of most of the data collection structures found in other languages, and since they are built...
Friday, May 6, 2022
Combining and separating strings
Strings can be combined (joined or concatenated) and separated (split) quite easily. Tokenization is the process of splitting something up into individual tokens; in this case, a sentence is split into individual words. When a web page is parsed by a browser, the HTML, JavaScript, and any other code...
Thursday, May 5, 2022
String formatting
Formatting strings is simply a way of presenting the information on the screen in a way that conveys the information best. Some examples of formatting are creating column headers, dynamically creating a sentence from a list or stored variable, or stripping extraneous information from the strings, such...
Wednesday, May 4, 2022
Indexing and slicing strings
Python strings functionally operate the same as Python lists, which are basically C arrays (see the Lists section). Unlike C arrays, characters within a string can be accessed both forward and backward. Frontward, a string starts off with a position of 0 and the character desired is found through an...
Tuesday, May 3, 2022
Strings
Strings in programming are simply text; either individual characters, words, phrases, or complete sentences. They are one of the most common elements to use when programming, at least when it comes to interacting with the user. Because they are so common, they are a native data type within Python, meaning...