Intro to Python Programming with Mosh - Pt. 02

Intro to Python Programming with Mosh - Pt. 02
March 14, 2019

Intro to Python Programming

Tutorial - 02

Youtube - Learn Python 3 for Machine Learning & Web Development [2019]
By: Programming with Mosh
Progress

End Time: 48:40

Next Part of Tutorial: Arithmetic Operations

NOTES

Strings

You can use single or double quotes for strings, but there is a difference for certain applications. For example, if you want an apostrophe in your string, you need to use double quotes to define the string. An example for the reverse is that you use single quotes to define the string if you want something to be in double quotes within the string.

Strings can also be defined with triple quotes. This allows you to create a multiline string.

You can call a character index of a string with brackets (i.e. string[0] will return the value of the first character in that string.). An interesting feature is that you can use negative indices to start at the end of the string and count backwards.

You can also call a chunk of characters with two values separated by a colon. If you don’t place a number before the colon, 0 will be assumed. If you don’t place a number after the colon, the length of the string is assumed. Using these together, you can perform a simple duplication of a string (i.e. another_string = string[:]).

The final check with this colon/index syntax was what would happen if you entered:
string[1:-1]
Where string was ‘Jennifer’. This returned ‘ennife’, which appears to indicate this syntax always returns values from left to right if possible, as well as showing that the beginning value is inclusive, while the final value is exclusive. To further test this, I tried string[3:1] and this did not return any value, which fit my thought on “left to right is possible”.

Formatted Strings

Formatted strings are used to make complex strings easier to visualize their output. Formatted strings are indicated with quotes starting with an f (i.e. f’text’). They then use curly brackets to create holes in the string where variables can be placed.

String Methods

The first basic method is len(). This returns the value of the number of characters in a string. This function is general purpose though, so it can count the number of objects in a list as well for example. The fact that it is general purpose is what makes it a function, as opposed to a method.

We looked into the find() method for strings. This takes an input of characters and returns the index of the first instance of those characters in the associated string. You can enter a single character or entire words to search for. Multiple characters will return the index of where those characters start.

The replace() method takes two inputs; the first input is what it searches for in the string, and the second input is what it replaces that with.

Next we used the “in” operator. Using the format of:
‘characters’ in string_variable
This returns a bool value of whether those characters are found within the string value indicated.

TERMINOLOGY

  • Method: in object oriented programming, a function that belongs to or is specific to some object

SUMMARY

The main difference between a function and a method is that a function is more general purpose, where as a method is something belonging to a specific type of object.

The string topics covered here gave me a lot of new information. I’m interested to see if there is any overlap with some of this in C# for Unity programming since there’s a lot of really helpful functions and methods covered. The formatted strings are especially nice to keep code nice and tidy instead of having those ugly string concatenation lines.

Comments

Popular posts from this blog

Online Multiplayer Networking Solution Tutorial Using Unity and Mirror - Tutorial by: Jason Weimann

Sebastian Lague A* Tutorial Series - Path Smoothing - Pt. 08 and Pt. 09

AStar for 2D Pathfinding in Unity