Python Tutorial for Beginners 2: Strings - Working with Textual Data

Corey Schafer
17 May 201721:11

Summary

TLDRThis video tutorial offers an in-depth exploration of Python's string data type, demonstrating how to create, manipulate, and utilize strings effectively. It covers variable naming conventions, string indexing, slicing, and methods like `lower()`, `upper()`, `count()`, `find()`, and `replace()`. The tutorial also introduces advanced string operations, including formatted strings, f-strings for Python 3.6+, and using `dir()` and `help()` functions to discover available string methods. Aimed at beginners, the video simplifies complex concepts, making Python's string handling accessible and engaging.

Takeaways

  • πŸ“ Python strings are used to represent textual data and are created using single or double quotes.
  • πŸ”‘ Python does not require semicolons to end statements; it relies on whitespace for structure.
  • πŸ”‘ Variable names in Python should be lowercase with words separated by underscores for readability.
  • πŸ“ The `len()` function in Python is used to find the length of a string.
  • πŸ‘‰ To access individual characters in a string, use indexing with square brackets, starting from index 0.
  • ❗ Attempting to access an index that doesn't exist in a string will result in an IndexError.
  • πŸ”„ String methods like `lower()`, `upper()`, `count()`, `find()`, and `replace()` allow for manipulation and analysis of string data.
  • πŸ”‘ The `dir()` function can be used to list all methods and attributes available for a given object in Python.
  • πŸ“š The `help()` function provides detailed information about Python's built-in functions and methods.
  • πŸ’¬ String concatenation can be achieved using the `+` operator or by using formatted strings for more complex constructions.
  • πŸ†• Python 3.6 introduced f-strings, which simplify the process of inserting variables into strings with ease.

Q & A

  • What is the purpose of the video?

    -The video aims to teach viewers about Python data types, specifically focusing on how to work with textual data using strings.

  • How do you create a variable to hold a text value in Python?

    -You can create a variable to hold a text value by assigning a string to it, such as `message = 'hello world'`.

  • Why does Python not require semicolons to end each line?

    -Python operates on whitespace, making it a clean language to work with, and does not require semicolons to end each line.

  • What are the conventions for naming variables in Python?

    -Variables are usually named in lowercase, and if they consist of multiple words, the words are separated by underscores. The names should be descriptive to indicate the value they hold.

  • How can you include a single quote within a string that uses single quotes?

    -You can either escape the single quote with a backslash (`\`), or you can use double quotes to enclose the string.

  • What is the difference between using single quotes and double quotes for strings in Python?

    -There is no functional difference between using single quotes and double quotes for strings in Python; it depends on the content of the string. If the string contains a single quote, use double quotes to avoid syntax errors and vice versa.

  • How do you create a multi-line string in Python?

    -You can create a multi-line string by enclosing the string in triple quotes (`'''` or `"""`).

  • How do you find the length of a string in Python?

    -You can find the length of a string using the `len` function, such as `len(message)`.

  • How do you access individual characters in a string?

    -You can access individual characters in a string using the index notation with square brackets, such as `message[0]` for the first character.

  • What is string slicing and how is it done?

    -String slicing is a way to access a range of characters in a string. It is done using the notation `string[start:stop]`, where `start` is inclusive and `stop` is exclusive.

  • What are some common string methods in Python?

    -Common string methods include `.lower()`, `.upper()`, `.count()`, `.find()`, and `.replace()`.

  • How do you concatenate strings in Python?

    -You can concatenate strings using the `+` operator, such as `greeting + ', ' + name`, or using formatted strings with placeholders.

  • What are formatted strings and how are they used?

    -Formatted strings allow you to create a string with placeholders that are replaced with variable values using the `.format()` method or f-strings (for Python 3.6+).

  • What are f-strings and how do they work?

    -F-strings are a way to embed expressions inside string literals using curly braces `{}`. They are prefixed with an `f` or `F` and allow for expressions within the placeholders, like `f'{name.upper()}'`.

  • How can you find all methods and attributes available to a string variable?

    -You can use the `dir(variable)` function to list all methods and attributes available to a string variable.

  • How can you get detailed information about a specific string method?

    -You can use the `help()` function with the string class and method name, such as `help(str.lower)`, to get detailed information about a specific string method.

  • What should you do if you want to replace a substring in a string?

    -Use the `.replace()` method, which takes two arguments: the substring to be replaced and the new substring, such as `message.replace('world', 'universe')`.

Outlines

00:00

πŸ“˜ Introduction to Python Strings

This paragraph introduces the concept of Python data types, focusing on strings which represent textual data. The script explains how to create a variable to hold text values and the importance of using descriptive variable names following Python's naming conventions. It also touches on Python's whitespace sensitivity and the use of single or double quotes for string literals, including handling strings with quotes within them by escaping or using alternative quotes. Additionally, it covers creating multi-line strings using triple quotes and accessing individual characters within a string using indices.

05:01

πŸ”’ String Manipulation and Methods

The second paragraph delves into string manipulation, starting with the use of the `len()` function to determine the length of a string. It explains how to access individual characters using square brackets and indices, and the concept of slicing to extract substrings. The paragraph also introduces string methods such as `.lower()`, `.upper()`, and `.count()`, demonstrating how to change the case of a string, count occurrences of substrings or characters, and handle errors when accessing non-existent indices.

10:03

πŸ” Finding and Replacing String Characters

This section discusses the use of the `find()` and `replace()` methods for locating and substituting characters within strings. It illustrates how the `find()` method returns the starting index of a substring, while the `replace()` method is used to swap out characters or substrings, with attention to the fact that `replace()` does not modify the original string but instead returns a new string with the replacements. The explanation includes the process of assigning the result of `replace()` to a variable to reflect the changes.

15:03

πŸ“ Concatenating and Formatting Strings

The fourth paragraph covers string concatenation using the plus sign operator and introduces the potential pitfalls of forgetting spaces between concatenated strings. It then demonstrates the use of formatted strings with placeholders for variables, utilizing the `.format()` method for a cleaner and more readable approach to string construction. The paragraph also highlights the use of f-strings available in Python 3.6 and above, which allow for simpler and more direct variable insertion within strings, and even the execution of expressions within placeholders.

20:04

πŸ› οΈ Exploring Python String Capabilities

The final paragraph provides insights on how to explore and understand the full range of methods and attributes available for strings in Python. It introduces the `dir()` function to list all available methods and attributes for a given object, and the `help()` function to provide detailed documentation on built-in functions and methods. The explanation emphasizes the utility of these functions for quickly accessing information about Python's string capabilities without needing to search online.

Mindmap

Keywords

πŸ’‘Python

Python is a high-level, interpreted programming language known for its readability and efficiency. In the context of the video, Python is the main subject, with a focus on its data types, particularly strings. The script demonstrates various operations and methods related to Python strings, showcasing the language's capabilities for handling textual data.

πŸ’‘Strings

In Python, a string is a sequence of characters enclosed in single or double quotes. The video script discusses strings extensively, explaining how they are used to represent textual data. Examples from the script include creating a string with the message 'hello world', manipulating it, and using it in various string methods.

πŸ’‘Variables

A variable in programming is a storage location paired with an associated symbolic name, which contains some known or unknown quantity or information, a value. In the script, the variable 'message' is used to store the string 'hello world', demonstrating how variables hold data that can be manipulated and used throughout a program.

πŸ’‘Whitespace

Whitespace in programming refers to any character or series of characters that represent spaces, tabs, or newlines in the text. The script mentions that Python operates on whitespace alone, meaning that the structure of the code is determined by the spaces, tabs, and newlines, rather than by semicolons or other delimiters.

πŸ’‘Indexing

Indexing is the method of accessing elements in a sequence, such as a string, by their position. The script explains how to access individual characters in a string by using their index numbers, starting with 0 for the first character. For example, accessing the character 'H' in 'hello world' is done by referring to the index 0.

πŸ’‘Slicing

Slicing is a way to extract a portion of a string by specifying a range of indices. The script demonstrates slicing with the 'hello world' example, showing how to extract the word 'hello' by using a colon to specify the start and end indices, such as message[0:5].

πŸ’‘Methods

In Python, a method is a function that is associated with an object and is called using the dot notation. The script introduces several string methods, such as 'lower()', 'upper()', 'count()', 'find()', and 'replace()', which are used to perform operations on strings, like changing case, counting characters, or replacing substrings.

πŸ’‘Concatenation

Concatenation refers to the operation of joining two strings together. The script shows how to concatenate strings using the plus sign operator, such as combining 'hello', a space, and 'world' to form 'hello world'. It also discusses potential issues with concatenation, like forgetting to add spaces between words.

πŸ’‘Formatted Strings

Formatted strings are strings that contain placeholders for values that are replaced with actual data at runtime. The script introduces the use of formatted strings in Python, where variables can be embedded within a string using curly braces and the 'format' method, making it easier to create readable and complex strings.

πŸ’‘f-strings

f-strings, introduced in Python 3.6, are a way to embed expressions inside string literals, using curly braces. The script highlights f-strings as a simpler and more efficient method for string formatting, allowing for variables and expressions to be included directly within the string, making the code more readable and concise.

πŸ’‘dir() and help() functions

The 'dir()' function in Python is used to list the methods and attributes of an object, while the 'help()' function provides documentation strings for objects. The script suggests using 'dir()' to explore available methods for a string and 'help()' to get detailed information about those methods, such as 'upper()' or 'lower()', providing a way for learners to discover and understand Python's capabilities.

Highlights

Introduction to Python data types, focusing on working with textual data (strings).

Demonstration of the print function with a 'Hello World' example.

Explanation of how to create a variable to hold text values.

Discussion on Python's syntax: no need for semicolons, using whitespace for clean code.

Variable naming conventions in Python: lowercase and underscores for multiple words.

Importance of using descriptive variable names for code readability.

Different ways to define strings using single and double quotes.

Handling single quotes within strings by escaping them or using double quotes.

Creating multi-line strings using triple quotes.

Using the len function to find the number of characters in a string.

Accessing individual characters in a string using indexing.

Explanation of string slicing to access a range of characters.

Introduction to string methods such as lower(), upper(), count(), and find().

Replacing parts of a string using the replace() method.

Concatenating strings using the plus sign operator and formatted strings.

Introduction to f-strings in Python 3.6 and above for easier string formatting.

Using dir() and help() functions to explore available string methods and attributes.

Encouragement to ask questions and interact in the comment section.

Suggestions to support the tutorials by liking, sharing, and contributing via Patreon.

Transcripts

play00:01

Hey there. How's it going everybody in this video?

play00:02

We'll be learning about python Data types and specifically

play00:04

We'll be learning about how to work with textual data and textual data in python are represented with strings

play00:10

So we currently have [opened] our intro pi file that we were working with in the last video

play00:15

Where we just printed out hello world and I'll go ahead and run this so that we can see that down here

play00:20

It does print out hello [world] [now]

play00:23

This line here is using the print function

play00:25

and we're passing this text value into that print function now if we wanted to create a

play00:30

Variable that holds that text value then we could say now

play00:34

I'll just get rid of this comment for now

play00:36

So if I wanted a variable to hold that value then I can just create a variable and we'll call that

play00:40

message and we'll set that message variable equal to our text value that we passed in to print and

play00:47

Here message is our variable [name]

play00:49

so if you're coming from another [language]

play00:51

Then you might be wondering if we need [to] use semicolons or something like that to end each line but python doesn't need that it

play00:57

operates on Whitespace alone

play00:59

Which makes it a very clean language to work with?

play01:01

Now by convention our variables are usually all lowercase and if it's multiple words then we separate those with underscore

play01:09

So if instead my variable name was my message, then it would be my underscore message

play01:14

So that's just a convention that's commonly used and also these variable names should be as descriptive as possible

play01:21

As to what values they're meant to hold so message is a good variable name here because it's holding our message

play01:27

But if I was instead to call this variable m which is a valid variable name

play01:32

But anyone reading my code now wouldn't know when they see this in variable

play01:37

What value it's supposed to hold so message is a much better variable name in this case

play01:42

So this message variable now holds our text data and our text data is called a string now

play01:48

We can use our variable in place of this string

play01:51

Anywhere that we use it in our program, so instead [of] printing out hello world

play01:56

Directly here. I'm instead going to now print out this variable and that should give us the same results

play02:01

So if I run that then you can see down here, we still get [the] same result now

play02:05

We can see that

play02:06

I created this string with single quotes now you can also use double quotes

play02:10

so if you're wondering if there is a difference between the single quotes in the double quotes

play02:14

And it really just depends on what text you have in your string

play02:18

So for example if you have a single quote in your string

play02:22

So for example let's create one with a single quote instead of hello world, [I] will instead make our string

play02:29

Bobby's World

play02:30

Now see the problem [here] is that?

play02:33

Python sees this single quote within our text as being the end of the string and it's not going to know what to do

play02:40

With what comes after that single quote here because it thinks that's where the string is closing

play02:46

So if you run this now then you'll get an error now

play02:49

There's a couple of different ways to [handle] this we could escape this single quote with a backslash

play02:55

so if I escape that single quote and now

play02:58

Run this now python knows that this single quote doesn't close out the string and that instead this one should

play03:05

Now another way to handle that is [to] instead

play03:08

So I'll take away that xscape character now another way to handle that is instead

play03:12

Just use double quotes on the outside of our string any string [that] contains single quotes

play03:18

So if we know that our string contains a single quote then we can instead use double quotes on the outside

play03:23

And then it'll know that that single quote isn't the end of the string so now if we run this then we can see that

play03:30

It still works fine

play03:30

But that doesn't necessarily mean that double quotes are better because it goes both ways if your string contains double quotes in the message then

play03:37

I would use single quotes on the outside now if we needed to create a

play03:42

multi-line string and one way we can do this is by using three quotes at the beginning and end of our string and

play03:48

these can also be

play03:50

Single or double quotes as [well], so let's go ahead and look at an example

play03:53

So I'll add three quotes to the beginning and end of our string here

play03:59

And now I'll just add [some] text to spans multiple Lines here

play04:02

so I'll just say was a good and then hit enter to go to a new line and say cartoon in the

play04:09

1990s so now if we run [that] and we can see that it printed out [our] string correctly over multiple lines, [okay]?

play04:17

So let's go back to our simple hello world example

play04:21

So I'm just going to take all of that and replace it with our previous

play04:25

Hello world example, and now let's go ahead and just run that really quick

play04:29

So we're back to our starting point so we can think of our string as a string of individual characters

play04:34

And we can access these individual characters also, so first let's see how we can find how many characters are in our string

play04:41

So to do this we can use the [len] function which stands for length

play04:46

So whenever I print out here if I was instead of printing my message if I was to print out this

play04:52

Length function and pass in message, and then run this. Now we're no longer printing out our message

play04:58

We're printing out the length of our message

play05:00

And we can see that it says that the length of our string is 11

play05:04

And if we counted these up 1 2 3 4 5 6 7 8 9 10 11

play05:10

Then we can see that that's correct, and we can access our strings characters individually, so to do this we can use the square

play05:18

Brackets after our string and pass in the location of the character that we want. So, I'll say print

play05:25

Message and then square brackets now the location is called an index and it starts out at 0

play05:31

So to access the first character of our string we can say

play05:36

print the message and then access this index of 0 so if we print that then we can see that we got the

play05:43

capital H now

play05:45

since the length of our string is 11 that means that with the first character starting out at 0 our last character would be at

play05:52

Index 10 so it's our total length minus 1 so if I was to say print out the

play05:59

Location at Index 10 the value at Index 10 then we can see that we got our d character

play06:05

Which is the last character in that string.

play06:07

Now if you accidentally try to access an index that doesn't exist then you'll get an index error. So if we were to say

play06:14

Access the index of 11, and we can see that that through an index error now

play06:19

We can also access a range of characters

play06:22

So if I just wanted to get the word hello from our string then we could say that we want

play06:29

0 and then this colon 5 and we'll explain this

play06:33

So the first index here is our starting point and the second in that index which is separated by this colon is

play06:40

The stopping point now one thing a little confusing about this is that the first index is

play06:45

Inclusive which means it's going to include that value, but the second index is not now

play06:50

There's good reasons for this

play06:52

But it's still easy to forget so basically what we're saying is I want all the characters

play06:57

Between the beginning and up to but not including the fifth index

play07:02

And it will be more clear if we just go ahead and run this so we can see that it prints out. Hello

play07:08

So it printed out our message from the zero index here all the way up to but not including

play07:14

The [5th] Index here, so we got hello now since our starting point. Here is just [the] first index

play07:21

We can actually just leave that off and it will assume that we want to start at the beginning

play07:25

So if we don't put anything there and then : [five] then we should get the same thing

play07:31

So if we run [that] we can see we still got hello

play07:33

now instead if we wanted to grab the word world from the string then we could start at

play07:40

The sixth Index and then we can just go to the end and just like leaving off our

play07:46

Starting index it will start from the beginning

play07:49

Leaving off the stop index just goes all the way to the end

play07:52

So now if we run that and we can see that it gives us back the word world now what we're doing

play07:58

Here is called slicing and if you'd like to learn more about

play08:01

Slicing in depth then you can watch my detailed video on that and I'll leave a link to [that] in the [description] section below

play08:07

So now let's just go back to printing out our message and let me run this okay

play08:12

So all of the data types that we're going to review are going to have certain methods available to us that give us access to

play08:18

a lot of useful functionality now when I say methods a lot of people wonder what's the difference between a method and a function and

play08:26

functions and methods are basically the same thing [a]

play08:29

Method is just a function that belongs to an object

play08:32

It's not important to get into the details of that now

play08:34

But if you hear me say method or function then you can basically think of those as the same thing for now

play08:40

So like I was saying the data types that we'll be going over all have certain methods

play08:45

Available to us that give us access to a lot of useful functionality so let's look at some of these string methods

play08:51

So we can see here that our hello world text is

play08:56

Capitalized, but let's say [that] we wanted that to be all lowercase now to do this

play09:01

It's just as easy as saying print message and then to lowercase this we can say dot lower

play09:09

Now when we run this dot lower with these parentheses here. That's running the lower method on the string

play09:15

So when we ran that we can see that now our hello world has been set to all lowercase [and] [if] we wanted to do

play09:21

this to uppercase and it's as easy as changing at

play09:24

Lower to upper if we run that now we can see that

play09:28

Hello World is all uppercase?

play09:29

So now let's say that we want to count a certain number of characters in our string and to do that

play09:34

We can use the count method and the count method actually takes a string as an argument so we can say

play09:40

Message dot count and now we have to pass in an argument

play09:45

And it has to know what we want to count in our message

play09:48

So what for now?

play09:49

We'll [just] say count hello

play09:50

And if we run that we can see that it returns that the string hello appears in our message one time

play09:56

But if we instead just passed in a single character as argument

play10:00

So I'll pass in an L if I run that

play10:03

You can see that we get a 3 because there are three L's and our message variable

play10:07

So if we instead want to find the index of where certain characters can be found and we can use the find method so I?

play10:14

Could come up here instead of saying dot count I can say not fine and now this takes an argument as well

play10:20

It's what we want to find so let me type in world here and run this

play10:25

And we can see when [we] run this that it returns a six

play10:28

and that's because world starts at the sixth index of our message variable now if we try to

play10:34

Find a string of characters that doesn't exist then it will just return negative one

play10:39

so if instead of world

play10:40

I typed in universe and ran that you can see it returns a negative one because it can't find that anywhere in our message variable

play10:48

Okay, so now let's say that we want to replace some characters in our string with some other characters

play10:53

And we can do this with the replace method now first. I'm going to change this change this back to printing out our

play11:00

regular message

play11:01

[so] I'll just delete these

play11:03

Now let's try to use our replace method right below where we first set our message fair herbal and this method takes two arguments

play11:11

First it takes what we want to replace so first let me just say message

play11:15

But replace so first it takes what we want to replace, so let's say that we want to replace

play11:22

world and now the second argument

play11:24

Which is separated by a comma [is] what we want to replace world with so we'll replace world with universe

play11:31

So now if [we] run this and this might be a little unexpected we can see that it's still printing out hello world

play11:38

[now] the reason our replacement didn't work is because it's not making that replacement in place

play11:44

It's actually returning a new string with those values replaced and we'll learn more about return statements when we learn about

play11:51

Functions, but basically we need to set a new variable here

play11:55

To get that returned string with those replacements, so I could say something like new message is

play12:02

equal to

play12:04

That original method with this replacement

play12:07

So now if we set that

play12:09

New variable and instead print that new message and run that and you can see that now it replaced the world with

play12:16

universe

play12:16

and if you really wanted to make the

play12:19

Replacement to the message variable then instead of making a new message variable and we can just set this same message

play12:26

Variable again, so now we're setting the same message variable equal to that replacement and then printing it out

play12:33

So if I run that and we can see that now the message variable had world

play12:37

Replaced with universe and this may look a little strange [to] set a variable to an altered [version] of itself

play12:43

But it's very common, and you'll be using that a lot

play12:46

Okay, so now let's get rid of this replace line here

play12:50

And now let's look at how we can add multiple strings and concatenate them together, so instead of saying hello world

play12:56

I'm instead just going to set this equal to hello and

play13:00

Instead of calling this message. I'm going to set this equal to greeting and just below greeting

play13:06

I'm going to create a variable called name and I'm going [to] set that equal to we'll just say

play13:11

Michael and now lastly let's create a message here and

play13:16

We want this message to combine our greeting with our name

play13:21

Wanted to say hello Michael and one way to do this is to use the plus sign operator

play13:27

so

play13:27

We could try this by saying greeting plus name now if we run this and we can see that

play13:34

It's not exactly what we wanted it combined them together, but it doesn't have a space there

play13:40

So when you're concatenated strings together it's easy to make mistakes like this

play13:44

So what we want to do is add a string between them so that spaces them out so we can add a string between these

play13:52

Just by putting in a string literal here

play13:55

And I'm also going to put in a comma and a space to separate those and now we also need another plus sign

play14:02

So that we're adding the name after that string so now we're saying that we want this greeting. Which is hello

play14:09

plus this comma and space plus the name so if I run this

play14:14

Then we can see that it concatenated those strings together

play14:16

How we wanted now sometimes using the plus sign isn't the best way to go if we wanted to create a longer more?

play14:23

Complicated string while using our variables and adding them all together like this

play14:28

Might get hard to keep track [of] so let's say that we wanted to add to the end of our message

play14:33

Just by you know closing off a sentence and saying

play14:37

Welcome so to do that after our name variable

play14:39

we could add another string that is a period to close off that sentence a space and then

play14:46

Welcome with an exclamation point so let's go ahead and run that now we can see that it printed out the string

play14:51

How we wanted it to look?

play14:53

But it's starting to get a little complicated on this line here to keep track of all of our plus signs and spaces within our

play15:00

message

play15:01

instead with strings like this

play15:03

It's usually better to use a formatted string

play15:06

[this] allows [us] to write the sentence as it will appear and put placeholders in place of our variables

play15:12

So let's go ahead and see what this would look like so though instead use a formatted string

play15:16

We can say message is equal to and we're just going to write it exactly how it appears

play15:22

Except everywhere where we want to

play15:24

Replace with a variable we're going to put in a placeholder and those placeholders are going to be these curly brackets

play15:30

so we want a placeholder for the greeting and then a comma a space and a placeholder for the name [and] a period and then

play15:39

[we'll] type out welcome and an exclamation point and now to fill those placeholders with our variables

play15:45

We can say dot

play15:47

format

play15:47

And pass in greeting for the first placeholder and [then] name for the second place holder

play15:54

So now let's go ahead and delete this line where we where we were using the plus sign

play15:59

Operator so if we run this we can see that

play16:01

We got the same [thing] as when we concatenate and then together but with longer more complicated strings

play16:07

This is actually a little bit easier to read it might look a little confusing now

play16:11

Since we're just seeing these placeholders for the first time but after you get used to this you will realize that this is

play16:16

Easier than keeping track [of] all those concatenations and using [stream] formatting like this also gives us some extra

play16:22

Functionality and if you want to see everything [that] you can do with that and I do have a detailed in-depth video on string

play16:28

Formatting and I'll add that to the description section below

play16:31

Ok and lastly if you're using python

play16:34

3.6 or above then you'll have access to these new things called f strings now if you aren't using

play16:41

[3.6] or higher then these aren't going to work because they were only

play16:44

Recently released and not a lot of people are using these f strings yet

play16:48

But I'm really liking them so far

play16:51

Basically the idea behind f strings was to make string formatting as simple as possible

play16:56

So let's go ahead and see what these look like so to say that [we] want this to [be] an f string

play17:01

We can just add an f right here to the beginning on the outside of the string

play17:06

now instead of using this dot format method well instead just write the variables directly within the

play17:12

placeholders so [I] can remove that dot format method and now we can pass the

play17:18

variables directly within the placeholder

play17:20

So we'll pass

play17:21

Greeting to that

play17:22

Placeholder and name to that one so I can save that and run it and you can see that using that we got the same

play17:27

Result [and] like I said these f strings are pretty new to the language so even people who are very familiar with python

play17:34

May be seeing this for the first time now one reason

play17:37

I really like these f strings is because you can actually write code within the placeholder

play17:42

So if I wanted the name and all caps for some reason then I could just come [into] the placeholder [here]

play17:48

And say name dot upper and run that directly within the placeholder if I run that

play17:53

Then you can see that now our name was capitalized, okay?

play17:57

So we're almost finished up

play17:58

But I wanted to show you one last trick here when you're learning something new in python

play18:03

So [we] saw a lot [of] different methods that we could use on our strings now if we ever wanted to see everything

play18:08

That's available to us then there are a couple of things that we can do so first we can use this

play18:14

dir function and that looks like this and

play18:18

what this does is if we pass in A

play18:21

Variable then it will show us all of the attributes and methods that we have access [to] with [that] variable now

play18:28

Don't worry about all of these double underscore methods yet, but if we look down here

play18:33

Then we'll see a couple of familiar methods that we used in this video

play18:38

[so] for example we have dot upper here we have

play18:42

not replace

play18:44

dot lower

play18:45

So this kind of gives you a broad overview of all the attributes and methods that are [available] to you now

play18:51

This doesn't show what any of these actually do so to see more information about these string methods

play18:57

Then we can use the help function

play18:59

But if we run this help function on the name then that won't work we actually need to run it on the string class

play19:08

Itself so we'll just type in help and then

play19:11

Str for string and if I run that and we can see that this gives us a lot more [information]

play19:17

And I'll pull this up here a bit

play19:19

so it

play19:20

You know tells us everything that's available to us, and if I scroll down here [a] bit

play19:25

And if I try to find something that we actually used in this video okay, so we have fine here

play19:30

So you can see that actually gives us a description of what these methods do and what arguments they take

play19:36

Now if you know that you had a certain method available to you

play19:40

But you couldn't remember exactly what it does then you can actually pass it directly in to help also, so if I wanted to

play19:48

Find out some more information about the lower method then we could say string dot

play19:54

Lower and if we run that and we can see that it gives us information and the description just of what lower does

play20:01

so using that dir function

play20:03

and that help function is a great way to get a broad overview of the methods and attributes that [are] available to you and

play20:09

Also, what using the help function gives you an idea of what those?

play20:14

Methods do without actually you know going online and checking everything there, okay?

play20:19

So I think that is going to do it for this video

play20:20

I hope that now you feel comfortable with

play20:23

Working with strings and are familiar [with] some of the useful things that we can do with those in the next video

play20:27

We're learning how to work with numbers in python and specifically integers and floats

play20:33

But if anyone has any [questions] about what we covered in this video

play20:35

Then feel free to ask in the comment section below and I'll do my [best] to answer those if you enjoy these tutorials and would

play20:41

Like to support them then there are several ways [you] can do that

play20:43

The easiest ways to simply like the video and give it a thumbs up and also

play20:47

it's a huge help to share these videos with

play20:48

Anyone you think would find them useful and if you have the means you can contribute through patreon

play20:52

And there's a link to that page in the description section below be sure to subscribe for future videos and thank you all for watching

play21:10

you

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
PythonStringsData TypesTextual DataString MethodsVariable NamingCode FormattingProgramming TutorialString IndexingString Slicingf-strings