ALL 47 STRING METHODS IN PYTHON EXPLAINED

Indently
7 Jan 202323:33

Summary

TLDRThis video script offers an extensive tutorial on Python's string methods, covering a total of 47 functions excluding dunder methods. It begins with common methods like 'capitalize' and 'upper', then delves into lesser-known functionalities. Each method is explained with examples, from simple case manipulations to complex string encoding and pattern searches. The script also clarifies the differences between methods that may seem similar, such as 'isdecimal', 'isdigit', and 'isnumeric'. The tutorial is rounded off with methods for string formatting, partitioning, and justification, providing a comprehensive guide for anyone looking to master string manipulation in Python.

Takeaways

  • 📝 The lesson covers 47 string methods in Python, excluding the dunder methods, providing a comprehensive overview of string manipulation capabilities.
  • 🔄 'capitalize' and 'casefold' methods are used to transform strings to start with a capital letter and to enable caseless comparisons, respectively.
  • 📏 'center' allows for centering a string within a given width, optionally padded with a specific character.
  • 🔢 'count' is utilized to determine the number of occurrences of a substring within a string.
  • 🔠 'encode' converts a string into a byte representation, with options to handle encoding errors.
  • 🔚 'endswith' checks if a string concludes with a specified character or tuple of characters.
  • 📖 'expandtabs' is used to convert tab characters into spaces, based on a defined tab width.
  • 🔍 'find' and 'index' locate the position of a substring within a string, with 'index' raising an error if the substring is not found.
  • 📐 'format' and 'format_map' offer ways to insert values into a string with placeholders and dictionary mappings, respectively.
  • 🔤 'isalnum', 'isalpha', 'isdigit', 'isdecimal', and 'isnumeric' are used to check for strings containing specific types of characters, with 'isalnum' being the broadest check.
  • 🛑 'index' is a method that, unlike 'find', will throw an error if the specified substring is not present in the string.
  • 🔗 'join' is used to concatenate elements of an iterable with a specified string as a separator.
  • 🔄 'lower' and 'upper' convert strings to lowercase and uppercase, respectively, while 'swapcase' swaps the case of each character.

Q & A

  • What does the 'capitalize' method do in Python?

    -The 'capitalize' method converts the first character of a string to uppercase and the rest to lowercase.

  • How does the 'casefold' method help in string comparison?

    -The 'casefold' method returns a case-insensitive version of the string, which helps in comparing strings without worrying about case differences.

  • What is the purpose of the 'center' method?

    -The 'center' method centers the string in a field of a specified width, optionally filling the surrounding space with a specified character.

  • How does the 'count' method work?

    -The 'count' method returns the number of non-overlapping occurrences of a substring in the string.

  • What is the function of the 'encode' method?

    -The 'encode' method encodes the string using the specified encoding and returns the encoded bytes object.

  • How can you check if a string ends with a specific suffix using Python?

    -You can use the 'endswith' method to check if a string ends with a specified suffix or any of a tuple of suffixes.

  • What does the 'expandtabs' method do?

    -The 'expandtabs' method replaces tabs in the string with spaces, using a specified tab size.

  • How do the 'find' and 'index' methods differ?

    -Both methods return the lowest index of the substring if found, but 'find' returns -1 if the substring is not found, while 'index' raises a ValueError.

  • What is the use of the 'format' and 'format_map' methods?

    -The 'format' method allows you to format strings using placeholders, while 'format_map' formats strings using a dictionary for the placeholders.

  • How do the 'isdecimal', 'isdigit', and 'isnumeric' methods differ?

    -The 'isdecimal' method checks if all characters are decimal characters, 'isdigit' checks if all characters are digits, and 'isnumeric' checks if all characters are numeric, including digits, decimals, and other numeric characters.

Outlines

00:00

📘 Introduction to Python String Methods

This paragraph introduces a comprehensive tutorial on Python's string methods. The speaker outlines the plan to cover all 47 string methods, excluding dunder methods, to ensure a complete understanding of string manipulation in Python. The 'capitalize' and 'casefold' methods are demonstrated with examples, showing how they can be used to manage case in strings. The 'center' method is also explained, illustrating how to center-align text within a specified width.

05:03

🔍 Exploring More String Methods in Python

Continuing from the introduction, this paragraph delves deeper into various string methods. The 'count' method is showcased for counting substring occurrences, while the 'encode' method is used to encode strings into bytes, with an optional error handling parameter. The 'endswith' method checks for ending characters or tuples of characters, and 'expandtabs' is used to convert tabs into spaces. The 'find' method locates the first occurrence of a substring, and 'format' is introduced for string formatting, both with named placeholders and without.

10:04

📚 Advanced String Method Applications in Python

The script now focuses on more advanced string methods. It explains the use of 'format_map' for dictionary-based string formatting, 'index' for finding the position of a substring with error-raising if not found, and 'isalnum', 'isalpha', 'isascii', and 'isdigit' for checking alphanumeric, alphabetic, ASCII, and digit-only strings. The paragraph also clarifies the differences between 'isdecimal', 'isdigit', and 'isnumeric', emphasizing their specific use cases.

15:07

🔑 Python String Methods for Validation and Transformation

This paragraph covers methods for validating and transforming strings. 'isidentifier' checks for valid Python identifier names, while 'lower' and 'upper' methods are used for case conversion. 'join' is introduced for concatenating elements of an iterable with a specified separator. 'ljust' and 'rjust' are explained for left and right text justification, and 'lower' is revisited for converting uppercase strings to lowercase. 'lstrip' and 'rstrip' methods are demonstrated for stripping characters from the left and right sides of a string.

20:09

🛠️ Additional String Manipulation Techniques in Python

The script continues with more string manipulation methods. 'maketrans' and 'translate' are paired for creating translation tables and applying them to strings. 'partition' and 'rpartition' are used for splitting strings based on a separator, with 'rpartition' starting from the right. 'removeprefix' and 'removesuffix' methods are introduced for removing specified prefixes or suffixes from strings. The paragraph also explains 'replace' for substituting substrings within a string and provides an example of limiting the number of replacements.

🔄 Reversing, Splitting, and Stripping Strings in Python

This paragraph discusses methods for reversing, splitting, and stripping strings. 'rfind' and 'rindex' are used for finding the last occurrence of a substring from the right, with 'rindex' raising an error if the substring is not found. 'rjust' and 'ljust' are revisited for justifying text within a given width. 'rpartition' and 'partition' are contrasted, with 'rpartition' focusing on the right side of the string. 'rsplit' and 'split' are introduced for splitting strings from the right and left, respectively, with 'maxsplit' controlling the maximum number of splits.

📖 Wrapping Up Python String Methods with Practical Examples

The final paragraph wraps up the tutorial with additional string methods. 'rstrip' is used for stripping characters from the right side of a string. 'splitlines' is introduced for splitting a string into lines based on newline characters, with an option to keep the newline characters. 'startswith' checks if a string starts with a specified substring. 'strip' is showcased for removing specified characters from both sides of a string. 'swapcase' is used for swapping the case of characters in a string. The 'title' method is highlighted for creating title-cased strings, and 'upper' is revisited for converting strings to uppercase. The tutorial concludes with 'zfill' for padding strings with zeros.

Mindmap

Keywords

💡String Methods

String methods are functions that can be called on string objects in Python to perform operations like manipulating, searching, or transforming the text. In the video, the instructor discusses various string methods, emphasizing their utility in text processing. For example, 'upper' and 'split' are common string methods mentioned, with 'upper' used to convert all characters in a string to uppercase and 'split' used to divide a string into a list of substrings based on a specified delimiter.

💡Capitalize

The 'capitalize' method in Python is used to convert the first character of a string to uppercase and the rest to lowercase. It is part of the string methods discussed in the video, which are essential for text formatting. The script provides an example where the string 'hello' is transformed to 'Hello' using this method, demonstrating its use in adjusting the case of text.

💡Case Fold

'Case fold' is a string method that returns a version of the string suitable for caseless comparisons, which means it adjusts the string so that case differences do not affect comparison operations. In the script, 'case fold' is used to normalize the string 'Mário' so that it can be compared equally to 'mario', disregarding letter casing and certain accent differences.

💡Center

The 'center' method in Python is used to center-align a string within a field of a given width. The method takes a number indicating the total width of the field and an optional fill character. In the video, the method is demonstrated by centering the text within a specified number of characters, using a fill character to pad the spaces on either side of the text.

💡Count

The 'count' method is used to count the number of occurrences of a substring within a string. It is one of the string methods highlighted in the video for searching and analyzing text content. The script illustrates this with an example where the substring 'AB' is counted within a string containing multiple instances of 'AB', returning the total count as the result.

💡Encode

The 'encode' method is used to encode a string using a specific character encoding. In the context of the video, 'encode' is demonstrated by converting a string into its byte representation using UTF-8 encoding. The method is crucial for handling text data in different formats and ensuring compatibility across various systems and applications.

💡EndsWith

The 'endswith' method checks if a string ends with a specified suffix. It is part of the string methods that help in pattern matching and validation. In the script, 'endswith' is used to verify if the word 'Apple' ends with the letter 'e', returning a boolean value based on the presence of the suffix at the end of the string.

💡Expand Tabs

The 'expandtabs' method is used to convert tab characters in a string to spaces, with an optional argument to specify the number of spaces to use for each tab. This method is useful for formatting text where tabs are used for indentation or alignment. The video script demonstrates 'expandtabs' by converting tabs to spaces in a given string, resulting in a visually aligned text block.

💡Find

The 'find' method is used to find the first occurrence of a specified substring within a string and return its index position. It is a fundamental string method for searching text. In the video, 'find' is used to locate the position of the word 'subscribe' within a given string, with the method returning the starting index of the first occurrence of the substring.

💡Format

The 'format' method in Python is used for string interpolation, allowing for the insertion of variables or values into a string. The method is versatile and can be used with positional or keyword arguments. In the script, 'format' is shown being used to replace placeholders in a string with actual values, such as replacing '{}' with 'cat' and 'meow', resulting in a readable sentence.

💡Join

The 'join' method is used to concatenate a list of strings into a single string, with an optional string argument that specifies the delimiter to insert between elements. It is a key method for combining text elements. In the video, 'join' is demonstrated by merging an array of strings into one string, separated by a hyphen, showcasing its use in creating compound strings from lists.

Highlights

Introduction to 47 string methods in Python, excluding dunder methods.

The 'capitalize' method to transform the first letter of a string to uppercase.

Using 'casefold' for caseless string comparisons.

'center' method to center-align strings with a specified width and fill character.

The 'count' method to find the number of occurrences of a substring.

'encode' method to convert strings into bytes using character encoding.

The 'endswith' method to check if a string ends with a specific character or tuple of characters.

'expandtabs' to convert tab characters to spaces with a defined tab width.

Using 'find' to locate the position of a substring within a string.

The 'format' method for string interpolation with keywords and arguments.

'format_map' to substitute formatted fields in a string using a dictionary.

'index' method to find the position of a substring, with error handling if not found.

The 'isalnum' method to check if a string contains only alphanumeric characters.

'isalpha' to verify if a string is composed solely of alphabetic characters.

'isascii' to determine if a string contains only ASCII characters.

The 'isdigit' method to check for a string that consists entirely of digits.

'isdecimal' to ascertain if a string represents a decimal number.

'isidentifier' to validate if a string is a valid Python identifier.

'islower' to check if all characters in a string are lowercase.

The 'isprintable' method to determine if a string contains only printable characters.

'isspace' to verify if a string contains only whitespace characters.

'istitle' to check if a string follows title capitalization rules.

'isupper' to determine if all characters in a string are uppercase.

The 'join' method to concatenate elements of an iterable with a specified separator.

'ljust' to left-align and pad a string to a given width with a fill character.

'lower' to convert all characters in a string to lowercase.

'lstrip' to remove leading characters or whitespace from the left side of a string.

Using 'maketrans' and 'translate' for creating translation tables and applying them.

'partition' to split a string into a tuple at the first occurrence of a separator.

'removeprefix' to eliminate a specified prefix from the beginning of a string.

'removesuffix' to remove a specified suffix from the end of a string.

'replace' to substitute occurrences of a substring with another substring.

The 'rfind' method to locate the last occurrence of a substring from the right.

'rindex' to find the last occurrence of a substring from the right with error handling.

'rjust' to right-align and pad a string to a given width with a fill character.

'rpartition' to split a string into a tuple at the last occurrence of a separator.

'rstrip' to remove trailing characters or whitespace from the right side of a string.

'splitlines' to split a string into a list of lines, with optional preservation of line endings.

'startswith' to check if a string starts with a specified prefix.

'strip' to remove leading and trailing characters or whitespace from both sides of a string.

'swapcase' to invert the case of each character in a string.

'title' to capitalize the first letter of each word in a string.

'upper' to convert all characters in a string to uppercase.

'zfill' to pad a numeric string with zeros on the left to fill a specified width.

Transcripts

play00:00

in today's lesson we're going to be

play00:01

going over all of the string Methods

play00:04

that come with the string in Python so I

play00:08

mean there's a lot of common ones such

play00:09

as split or upper that you are probably

play00:12

familiar with but there are also a lot

play00:15

that aren't really that commonly used

play00:18

and I want to just cover every single

play00:20

one of these so there are going to be 47

play00:23

in total excluding the dunder methods so

play00:27

in case you're curious about everything

play00:29

you can do with a string follow along

play00:31

with this video because we're going to

play00:32

be covering 47 of these string Methods

play00:36

for number one we have capitalize which

play00:38

means if we have some text and that's in

play00:41

lowercase or uppercase we can capitalize

play00:44

that and

play00:46

when we run this we're going to get

play00:48

hello with a capital H and if this is in

play00:52

uppercase it's still going to capitalize

play00:54

that string next we have case fold which

play00:58

returns a version of the string for

play01:00

caseless comparisons so pretend you have

play01:03

Mario here that's spelled a bit weird if

play01:05

we run this it's going to make sure that

play01:08

we can compare this to another Mario

play01:10

without having to worry about whether

play01:12

this is uppercase or lowercase so if we

play01:15

duplicate this line and say text 2 and

play01:17

change this to Mario

play01:20

like that as you probably know this is

play01:23

not going to be equal to this because of

play01:25

the case sensitivity but if we print

play01:27

both of them text 1 and text two they're

play01:30

both going to be processed into a

play01:32

version that can be compared without

play01:34

having to worry about the case next we

play01:37

have the center method so suppose we

play01:40

have some text and we want to call Dot

play01:43

Center on it provide some sort of number

play01:45

and that number is going to specify how

play01:48

many characters you want this string to

play01:51

occupy and where you want the text to be

play01:53

centered and optionally you can include

play01:56

a character or a string that's going to

play02:00

be placed around the string that you

play02:02

want to Center number four is Count

play02:04

suppose you have a string with some sort

play02:08

of substring that you want to count here

play02:11

if we type in ABC underscore ABC

play02:13

underscore ABC and so on and then we use

play02:15

the method dot count with only a b we're

play02:18

going to get a return of 4 because it's

play02:21

counting all the occurrences of a b in

play02:24

this string for number five we have

play02:27

encode which allows us to encode a

play02:30

string of our choice and if we run this

play02:33

we're going to get the byte version of

play02:36

Elon Musk because we were able to encode

play02:38

that into utf-8 and you can also provide

play02:42

an optional parameter which says errors

play02:44

and if you set this to strict it means

play02:47

that if it cannot encode it it's going

play02:49

to throw an error which is good in case

play02:51

you want to make sure that this does

play02:54

become encoded for number six we have

play02:57

the ends with method which checks that a

play02:59

string ends with a certain letter such

play03:03

as e if we check now that Apple ends

play03:06

with e we're going to get a return of

play03:09

true and this doesn't have to be a

play03:11

single character it can actually be a

play03:13

tuple of characters such as e and we can

play03:16

also say a so now it's going to check

play03:18

that it ends with either e or a and if

play03:22

we run that we're still going to get

play03:23

true and if we type in apla it will

play03:26

still be true but if we type in

play03:28

something such as Zed it's going to

play03:30

return false because neither e nor a was

play03:32

at the end of our string number seven

play03:36

expand tabs right here we have a string

play03:39

with some tabs as you can see by the

play03:41

backslash T using dot expand tabs allows

play03:45

us to expand those tabs using spaces so

play03:48

here I defined a value of 20 which means

play03:51

when we run this you'll see that we'll

play03:52

get some very long tabs inside our

play03:55

string number eight find

play03:58

suppose we have a string that says

play04:01

something silly such as remember to

play04:03

comment and subscribe and suppose we

play04:06

want to find the substring of subscribe

play04:09

in this string well we can do that using

play04:11

the text Dot find method and it's going

play04:14

to return to us the integer of the first

play04:16

and lowest occurrence of subscribe so if

play04:20

we have multiple subscribes it's going

play04:22

to return the first one that it finds

play04:24

now if we run this we're going to find

play04:27

that subscribe was located at the index

play04:29

of 24 so I went ahead and printed

play04:32

everything after that position and that

play04:35

returned to me subscribe

play04:37

now if we don't have what we are looking

play04:39

for such as as does it's going to return

play04:42

to us the index of negative one number

play04:45

nine format here are two different ways

play04:49

we can use this method with strings the

play04:52

first way is defining some keywords

play04:54

inside the string followed by some curly

play04:57

brackets and then typing in text.format

play04:59

followed by the keyword arguments such

play05:02

as

play05:03

subject and action which I've included

play05:06

in this string now subject is going to

play05:08

be replaced by cat and action is going

play05:10

to be replaced by meow and the same

play05:12

thing works without having to specify

play05:15

the keywords now when we run both of

play05:17

these it's going to say cat is doing

play05:19

meow next we have format map which Maps

play05:23

a dictionary to the formatted values

play05:26

that we have in our string so for

play05:28

example if we have a dictionary with X

play05:31

and Y and then we have a string using

play05:33

those values we can just call text Dot

play05:36

format map coordinates and it's going to

play05:38

place these values in the given values

play05:40

down here so if we run this we'll get

play05:43

that the coordinates are 10 and -5

play05:45

number 11 Index right here we have a

play05:49

string that says astronauts recently

play05:51

discovered a banana on the moon question

play05:54

mark and we want to find the position of

play05:57

banana now recently I showed you that we

play05:59

can use find to find the position of a

play06:03

certain substring and we can achieve the

play06:05

same thing with index except index does

play06:08

have one difference and that is that if

play06:10

we do not find what we're looking for

play06:12

it's going to throw an error instead of

play06:15

returning to us minus one number 12

play06:18

is al-num which stands for is

play06:21

alphanumeric and that checks that the

play06:23

string contains letters or numbers but

play06:26

if it contains something such as an

play06:29

exclamation mark or some other symbol

play06:30

it's not going to be considered

play06:32

alphanumeric anymore so right now if we

play06:34

run this with hello kitty123 it is

play06:37

alphanumeric because it only contains

play06:39

letters and numbers but if we add an

play06:41

exclamation mark for example it's going

play06:43

to evaluate two false because an

play06:46

exclamation mark is neither a number nor

play06:49

a letter number 13 is Alpha and that

play06:52

checks that the text is alphabetic only

play06:55

so if we run this it's going to return

play06:57

false because Hello Kitty contains

play07:00

numbers but if we remove the numbers

play07:01

it's going to evaluate to True number 14

play07:04

is Ashi and that returns to us true if

play07:09

the current string contains only ashy

play07:11

characters so if we decide to add these

play07:13

symbols such as a copyright Mark to our

play07:16

text it's going to return false to us

play07:18

because that is not part of the Ashi

play07:20

table but if we exclude that it's going

play07:23

to return true to us now for 15 16 and

play07:26

17 I decided to bring in my notes for

play07:29

this because these are quite confusing

play07:31

if you don't have them side by side so

play07:33

what we're covering now is is decimal is

play07:36

digit and is numeric so right now if you

play07:39

want to make sure that something is

play07:41

actually a number you can use is decimal

play07:44

and that's going to evaluate one two

play07:45

three to true and if you run that the

play07:49

first one is going to evaluate to true

play07:52

but if we decide to evaluate something

play07:54

that is numeric but not a decimal such

play07:57

as these crazy symbols here it's going

play07:59

to evaluate to false and that is because

play08:02

it is only checking that this is a

play08:04

decimal now is digit checks for

play08:07

something very similar it checks that

play08:09

the current string is a digit and one

play08:13

two three four five six even in these

play08:15

crazy bubbles is considered a digit so

play08:17

it's going to evaluate to true

play08:21

now numeric on the other hand is going

play08:24

to just evaluate the true if the number

play08:26

happens to be numeric so all these

play08:29

Japanese numbers do evaluate the numbers

play08:30

so it's considered numeric but this does

play08:33

not work if you put it inside here

play08:35

because it is not a digit

play08:38

now the reason I brought all three of

play08:40

these together is just to show you that

play08:42

they actually work in quite similar ways

play08:45

and that is that is numeric works with

play08:47

all three is digit works with these two

play08:49

and is decimal only works with numbers

play08:53

so what I'm trying to say is that if you

play08:55

wanted to include some numbers inside is

play08:58

numeric such as one two three it will

play09:00

still evaluate to true

play09:02

if you wanted to include some crazy

play09:04

symbols like these ones that are digits

play09:07

it will still evaluate to true

play09:09

but this does not work the other way

play09:11

around so if we take these Japanese

play09:13

numbers and place them inside the is

play09:15

digit it's going to evaluate two false

play09:18

because it is not a digit even though it

play09:21

is numeric but for the most part of the

play09:23

time you probably just want to check

play09:25

that it is a decimal so I would

play09:27

recommend using that but in case you

play09:30

want to check whether something is

play09:31

numeric or is a digit you also have that

play09:34

option

play09:35

number 18 is identifier

play09:38

here we have some text that says test

play09:41

sample and what this does is check that

play09:43

it is a valid identifier name in Python

play09:46

in case you want to use it to name a

play09:48

variable for example if we run this

play09:50

we're going to get false because you

play09:52

cannot use dashes in names in Python if

play09:55

we change it to an underscore it will

play09:57

work but if we change this to a one test

play10:00

underscore sample it will still return

play10:01

false because numbers cannot proceed

play10:04

variable names in Python number 19 is

play10:07

lower here we have some text with ABC

play10:10

with a uppercased and what this does is

play10:12

check that the entire string is

play10:14

lowercased if we run this we're going to

play10:17

get false as a return because ABC has a

play10:19

capital A but if we return it with a

play10:22

lowercase it's going to evaluate to True

play10:25

number 20 is printable all this does is

play10:29

check that the current line of text is

play10:31

actually printable and it considers text

play10:34

to be printable if it doesn't contain

play10:36

any Escape characters such as new line

play10:39

if we run this right now we're going to

play10:41

get false because it contains this

play10:42

escape sequence but if we return it

play10:45

without that escape sequence we're going

play10:47

to get true as a return number 21 is

play10:50

space all this does is check that a

play10:53

string only contains space so if we

play10:56

create this string with a lot of empty

play10:58

space and run the program we're going to

play11:00

get true as a return because it is only

play11:03

space

play11:05

number 22 is title is title checks that

play11:09

the given string is a title so that

play11:12

video is going to elaborate to true

play11:15

because it is a title it follows the

play11:18

title syntax if we type in something

play11:20

such as hello video it's going to

play11:22

evaluate the false because hello is not

play11:24

uppercased so all it's really doing is

play11:27

checking that that the first letter of

play11:28

each word is uppercased number 23 is

play11:32

upper here we have a string with bananas

play11:35

in complete uppercase and all this does

play11:38

is check that the entire string is

play11:40

uppercased here we're going to get a

play11:43

return of true because bananas is

play11:45

uppercased but if we add a lowercase n

play11:48

it's going to evaluate the false because

play11:50

it is not entirely uppercased number 24

play11:53

join right now we have a text of type

play11:56

string and we want to combine some words

play11:59

for example so what join does is combine

play12:02

an array of items together using the

play12:05

given string so this is going to be

play12:07

placed in between each one of the

play12:09

elements that we are joining and to call

play12:11

it we just have to say text Dot join and

play12:14

we're going to join an array of our

play12:16

choice so if we run this we'll get text

play12:19

1 dash text 2-text3 and you will

play12:22

probably see it without this because it

play12:24

is an extra line of code that you don't

play12:26

need and used instead like this with a

play12:30

dash and with that we will still get the

play12:32

same result but without having to use

play12:34

this extra variable next we have L just

play12:37

which aligns the text as far as possible

play12:40

to the left with the amount of space

play12:42

that we've provided so here we have

play12:45

text.eljust and I decided to give it 20

play12:47

characters of space and to fill the

play12:49

remaining with the underscore so if we

play12:52

run this we're going to get text and

play12:54

underscore field for the remaining spots

play12:57

inside this string number 26 lower right

play13:01

now we have a string that has been

play13:03

uppercased and all we want to do is

play13:06

convert it to lowercase and we can use

play13:08

that using text Dot lower so uppercase

play13:12

in uppercase is going to become an

play13:14

uppercase in lowercase number 27 L strip

play13:18

here we have some text and pretend you

play13:21

want to remove some from some text we

play13:24

can do that using dot L strip followed

play13:26

by the keyword or the text that you want

play13:28

to remove from the left side of the

play13:31

string so if we remove that we will just

play13:34

have text remaining and if we do sum

play13:37

with some space it's also going to

play13:39

remove the space in front of text number

play13:42

28 and 29

play13:44

make trans and translate these two tend

play13:47

to go hand in hand when you are working

play13:49

with them because what make trans does

play13:51

is create a translation table so right

play13:54

now what we're saying is that we want to

play13:56

convert this text to a translation table

play13:59

and to do that we want to make sure that

play14:01

every uppercase b gets translated to

play14:04

this Emoji over here and then we want to

play14:07

use that translation table for the text

play14:09

that we want to print because right now

play14:11

if we print this text as it is even if

play14:13

we've created a table it's not going to

play14:15

do anything but if we translate it with

play14:18

the table that we've created here it's

play14:20

going to replace each uppercase B with

play14:22

this emoji

play14:24

and if we run that we'll see that 66 or

play14:27

that character was replaced with this

play14:29

emoji character and that's why we're

play14:31

going to get this funky string over here

play14:33

and the cool thing about that is that

play14:36

you can use any new string you want it

play14:38

can even be that is bacon

play14:40

baby

play14:41

and when you run this we're going to get

play14:44

each one of the uppercase B's replaced

play14:46

with this symbol over here if you want

play14:50

to have more than just one character

play14:51

converted you can enter your own

play14:53

dictionary key and it's going to create

play14:55

a translation table number 30 partition

play14:58

suppose you have a string such as a plus

play15:01

b equals c and you want to partition

play15:04

these in other words split them at a

play15:07

certain point you can use the dot

play15:09

partition method to make sure that you

play15:11

split this into three equal strings at

play15:14

the index of equal and what that really

play15:16

means is that we're using equals as a

play15:18

separator to tell the program that we

play15:20

want to split this string into three

play15:22

parts between the equal sign so we're

play15:24

going to get back a plus b equals and C

play15:27

number 31 remove prefix here we have a

play15:31

string that says

play15:33

and what we want to do is remove the

play15:35

beginning part which says was to do that

play15:38

we just type in dot remove prefix and

play15:41

all we're going to get back is app now

play15:43

for number 32 it's exactly the opposite

play15:46

we are removing the suffix so here we

play15:48

have Mr everyone and we want to remove

play15:51

one from this so here we'll just type in

play15:53

text Dot remove suffix with one and when

play15:56

we run that we'll get Mr every now

play15:59

number 33 is one of the most important

play16:01

ones because it tells you to remember to

play16:04

comment I'm just joking about that of

play16:06

course but here we have a string that

play16:08

says remember to comment and all we're

play16:10

going to do is replace it with subscribe

play16:13

so here you can choose a substring that

play16:16

you want to replace and replace it with

play16:18

the current string that you've provided

play16:20

so if we run that instead of saying

play16:22

remember to comment it's going to say

play16:24

remember to subscribe and optionally in

play16:27

case you have many occurrences of the

play16:29

same word you can provide a limit to how

play16:33

many times you want to replace that text

play16:36

so instead of replacing every occurrence

play16:38

of comment with subscribe we're only

play16:40

going to replace the first occurrence so

play16:43

now it's going to say remember to

play16:44

subscribe and to comment

play16:47

number 34 are find here we have some

play16:50

text that says a some text Dot a and

play16:54

what we want to do is find the first

play16:56

occurrence of a starting from the right

play16:58

so here if we type in text Dot our find

play17:01

and we print the position we're going to

play17:03

get the latter and not the first if we

play17:06

use the original we're going to get the

play17:09

first one

play17:11

and just as before if you type in

play17:13

something that doesn't exist you're

play17:15

going to get -1 when you use find when

play17:18

you use index you're going to get an

play17:20

error

play17:21

but what better way to explain that than

play17:24

using the exact same example except with

play17:27

our index which does the same thing as

play17:29

index except it starts from the end and

play17:32

searches in this direction so if we look

play17:35

for B it's going to return to us 14

play17:37

because it's starting from the right

play17:39

side and again if you type in something

play17:41

that doesn't exist it's going to give us

play17:43

a value error number 36 are justify here

play17:48

we have some text and we're just going

play17:49

to say text dots are just and we want to

play17:52

make sure that this string occupies 20

play17:53

spaces and we're going to fill the

play17:55

remaining with the underscore and as you

play17:58

can see in the output that's what R just

play18:01

does now at this point you know what

play18:03

partition does it splits the string into

play18:06

three depending on the separator that

play18:08

you've defined but we also have

play18:11

something called R petition which tells

play18:13

us which tells the program to start from

play18:15

the right side so instead of getting

play18:17

text and having this as a divider we're

play18:20

going to be using the second one instead

play18:22

because it use because it starts from

play18:24

the right side and if we run that you'll

play18:26

see text equals text 2 equals but if we

play18:29

remove the r from our partition it's

play18:32

going to do it the opposite way

play18:35

38 and 39 are splits and split but first

play18:40

we're going to cover our split

play18:43

so here we have some text that says this

play18:46

is some special stuff and what our split

play18:49

does is starting from the right it

play18:51

splits the string into an array given

play18:53

the separator that we've provided

play18:56

so as you can see here we have some

play18:57

space that we decided to use as a

play18:59

separator so it's going to split each

play19:01

and every one of these words into its

play19:04

own element and if we run that we'll get

play19:06

this is some special stuff because we

play19:08

used the space as a separator

play19:12

and the same thing goes for this website

play19:13

over here we decided to use a DOT as a

play19:16

separator so it's separated w w website

play19:20

and com

play19:22

now text.split is the exact same thing

play19:24

except it evaluates this from the left

play19:27

side so it starts splitting in this

play19:29

direction which means that if we run

play19:31

this program right now and provide a Max

play19:34

split of two it's going to split this

play19:36

two times at most so this is some

play19:40

special stuff as you can see it

play19:42

separated these two but it did not

play19:43

separate the rest because we said we're

play19:46

only going to separate the first two

play19:47

elements and the reason I used Max split

play19:50

here is because I wanted to show you

play19:51

that if we use R split with Max split

play19:54

it's going to do it in the opposite

play19:56

direction because it starts from the

play19:58

right side first next we have R strip

play20:01

which strips the text from the right

play20:03

side so here we have text.r strip and we

play20:06

want to strip Mario from the right side

play20:08

and since we have two Marios here it's

play20:10

going to remove this Mario right there

play20:13

and now we'll have an output of his name

play20:16

is Mario 41 we have split lines so

play20:20

suppose you have some text and the text

play20:23

has some separators such as slash n

play20:26

which starts a new line text Dot split

play20:29

lines is going to take each one of those

play20:31

lines and put it as an element into an

play20:34

array so if we run this we'll get

play20:36

remember to comment as two different

play20:38

elements in an array

play20:41

now you can specify this parameter keep

play20:43

ends to be true or false depending on

play20:45

whether you want to keep these escaped

play20:48

characters without it you're just going

play20:50

to get D strings without the backslash n

play20:53

included in both of the elements number

play20:56

42 starts with suppose you have a string

play21:00

that starts with L you can check that by

play21:02

using the dot starts with

play21:04

method and it's going to check that

play21:07

Luigi starts with L which is true but

play21:10

Luigi does not start with f and that can

play21:13

be proven by using this method number 43

play21:16

strip suppose you want to remove Luigi

play21:19

from this string you can just type in

play21:21

text Dot strip and here we're just

play21:23

removing Luigi by inserting Luigi so by

play21:26

running this we will effectively remove

play21:28

Luigi and this one works for both sides

play21:31

so you can either remove pasta or Luigi

play21:33

depending on your preference as you can

play21:36

see if we type in past that dot it will

play21:39

also remove pasta number 44 swap case

play21:42

here we have a string that says Luigi

play21:45

has pasta and let's just swap case that

play21:48

the easiest way to explain this is to

play21:50

run it now we have Luigi has pasta in a

play21:53

swap case

play21:54

now one that's really interesting in

play21:57

Python that can help you a lot in case

play21:58

you don't know how to write a title is

play22:00

the dot title method here I wrote

play22:03

something that's called This is a title

play22:05

and what this is a title do is make sure

play22:08

that each letter is uppercased in your

play22:10

title so I would say simple way to make

play22:12

a title and I think this is one of my

play22:14

favorite string Methods because of it

play22:16

number 46 upper here we have some text

play22:20

that says hello there and we just want

play22:22

to uppercase that and if we run this

play22:24

we'll get hello there in screaming

play22:26

characters and finally we have number 47

play22:29

which is also the final method that we

play22:31

will be covering in this video and that

play22:34

is zero fill so right now we have some

play22:37

text and if we just run this as it is

play22:40

you'll see that text is going to be

play22:42

filled with some zeros just in case you

play22:46

have to add some zeros to your text but

play22:49

anyways those were 47 string Methods

play22:51

that you can use in Python do let me

play22:54

know in the comment section down below

play22:55

if you learned something new or if there

play22:57

was something that I missed regarding

play22:59

these methods this video took me 50

play23:01

minutes to record and it's actually even

play23:03

growing longer because of this

play23:04

explanation but if you did enjoy the

play23:07

video do consider leaving a like and a

play23:10

comment I love to read those but

play23:12

otherwise with that being said I also

play23:13

want to wish all of you a Happy New Year

play23:15

I know this is quite late maybe it's day

play23:18

five day six of the year but this is the

play23:21

first video I'm recording this year so I

play23:23

thought I might as well mention it

play23:24

otherwise with that being said as always

play23:27

thanks for watching and I'll see you in

play23:28

the next video

play23:31

oh

Rate This

5.0 / 5 (0 votes)

الوسوم ذات الصلة
PythonString MethodsText ManipulationData ProcessingProgramming TutorialCode TipsString OperationsDeveloper ToolsLearn PythonCoding Techniques
هل تحتاج إلى تلخيص باللغة الإنجليزية؟