Python 3 Strings - Methods

Python 3: From Scratch to Intermediate BEGINNER LEVEL: Python 3 - Strings
8 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€93
List Price:  €130.20
You save:  €37.20
£80.04
List Price:  £112.06
You save:  £32.01
CA$137.46
List Price:  CA$192.45
You save:  CA$54.99
A$152.16
List Price:  A$213.03
You save:  A$60.87
S$135.55
List Price:  S$189.78
You save:  S$54.22
HK$781.57
List Price:  HK$1,094.24
You save:  HK$312.66
CHF 90.85
List Price:  CHF 127.19
You save:  CHF 36.34
NOK kr1,092.22
List Price:  NOK kr1,529.15
You save:  NOK kr436.93
DKK kr693.66
List Price:  DKK kr971.15
You save:  DKK kr277.49
NZ$166.97
List Price:  NZ$233.77
You save:  NZ$66.79
د.إ367.25
List Price:  د.إ514.17
You save:  د.إ146.91
৳10,995.41
List Price:  ৳15,394.02
You save:  ৳4,398.60
₹8,350.38
List Price:  ₹11,690.87
You save:  ₹3,340.48
RM474.20
List Price:  RM663.90
You save:  RM189.70
₦140,430.95
List Price:  ₦196,608.95
You save:  ₦56,178
₨27,811.82
List Price:  ₨38,937.66
You save:  ₨11,125.84
฿3,694.61
List Price:  ฿5,172.60
You save:  ฿1,477.99
₺3,225.42
List Price:  ₺4,515.72
You save:  ₺1,290.29
B$507.43
List Price:  B$710.43
You save:  B$202.99
R1,857.85
List Price:  R2,601.07
You save:  R743.21
Лв182.19
List Price:  Лв255.08
You save:  Лв72.88
₩136,210.38
List Price:  ₩190,699.98
You save:  ₩54,489.60
₪370.57
List Price:  ₪518.82
You save:  ₪148.24
₱5,735.07
List Price:  ₱8,029.33
You save:  ₱2,294.25
¥15,537.84
List Price:  ¥21,753.60
You save:  ¥6,215.76
MX$1,691.01
List Price:  MX$2,367.48
You save:  MX$676.47
QR364.06
List Price:  QR509.70
You save:  QR145.64
P1,360.66
List Price:  P1,904.98
You save:  P544.31
KSh13,098.69
List Price:  KSh18,338.69
You save:  KSh5,240
E£4,756.64
List Price:  E£6,659.49
You save:  E£1,902.84
ብር5,704.16
List Price:  ብር7,986.06
You save:  ብር2,281.89
Kz83,693.62
List Price:  Kz117,174.42
You save:  Kz33,480.80
CLP$93,652.63
List Price:  CLP$131,117.43
You save:  CLP$37,464.80
CN¥722.46
List Price:  CN¥1,011.48
You save:  CN¥289.01
RD$5,816.45
List Price:  RD$8,143.26
You save:  RD$2,326.81
DA13,451.28
List Price:  DA18,832.33
You save:  DA5,381.05
FJ$227.46
List Price:  FJ$318.45
You save:  FJ$90.99
Q778.38
List Price:  Q1,089.77
You save:  Q311.38
GY$20,959.74
List Price:  GY$29,344.48
You save:  GY$8,384.73
ISK kr13,978.60
List Price:  ISK kr19,570.60
You save:  ISK kr5,592
DH1,004.51
List Price:  DH1,406.36
You save:  DH401.84
L1,772.88
List Price:  L2,482.10
You save:  L709.22
ден5,732.06
List Price:  ден8,025.12
You save:  ден2,293.05
MOP$807.08
List Price:  MOP$1,129.95
You save:  MOP$322.86
N$1,848.73
List Price:  N$2,588.30
You save:  N$739.56
C$3,687.79
List Price:  C$5,163.05
You save:  C$1,475.26
रु13,385.83
List Price:  रु18,740.70
You save:  रु5,354.86
S/373.68
List Price:  S/523.18
You save:  S/149.49
K387.85
List Price:  K543
You save:  K155.15
SAR374.99
List Price:  SAR525.01
You save:  SAR150.01
ZK2,715.07
List Price:  ZK3,801.21
You save:  ZK1,086.13
L462.80
List Price:  L647.94
You save:  L185.14
Kč2,329.86
List Price:  Kč3,261.90
You save:  Kč932.03
Ft36,210.88
List Price:  Ft50,696.68
You save:  Ft14,485.80
SEK kr1,090.42
List Price:  SEK kr1,526.63
You save:  SEK kr436.21
ARS$88,067.42
List Price:  ARS$123,297.91
You save:  ARS$35,230.49
Bs691.01
List Price:  Bs967.44
You save:  Bs276.43
COP$390,100.45
List Price:  COP$546,156.24
You save:  COP$156,055.78
₡51,233.50
List Price:  ₡71,728.95
You save:  ₡20,495.44
L2,475.71
List Price:  L3,466.09
You save:  L990.38
₲747,614.58
List Price:  ₲1,046,690.32
You save:  ₲299,075.74
$U3,836.09
List Price:  $U5,370.68
You save:  $U1,534.59
zł401.29
List Price:  zł561.83
You save:  zł160.53
Already have an account? Log In

Transcript

Okay, we've talked about indexing and how we can determine the length of a sequence in our cases string using the Len function. Now let's see other operations on strings. First, one more thing about indexes, you can find out the index of a character inside the given string by using the index method, just remember that this method returns only the first occurrence of that particular character in the string. So let's create a variable a and assign it the string Cisco switch, you can clearly see that the letter AI appears two times inside the string. So let's find out the index of the first occurrence of I in the string, a dot index of I. So we have the name of the variable associated with the string, then a dot, then the name of the method, index and then in between offices, we enter the character we want to find out the index for I don't forget to enclose it in single or double quotes, since this letter is also of type string.

Okay, so this returns one as being the index of I in the string, which is correct since it is the second letter inside the string. Another useful Python method is one that helps you find out how many times does a character appear in a string, or, generally speaking, an element inside a particular sequence. This method is called count. The syntax of count is similar to the one of the index method you've seen earlier. To use the count method, just type in the name of the variable, so a, then a dot, then the word count, then open and close parentheses. And finally, the letter you want to count surrounded by quote.

So a count of I returned to so we have I Two times inside our string, which is correct. Another string method is find this method simply searches for a sequence of characters inside the string. And if it finds a match, then it returns the index where that sequence begins. So let's see that let's type in a dot find, and let's say SEO. So the result indicates a match starting at index two, which is correct 012, since the letter S is positioned at index two within the string. On the other hand, if Python does not find a match, then it will return the value minus one.

So let's save this. Let's search for example for the string XYZ within our string, Cisco switch and we see minus one being returned. That's because we don't have the substring XYZ within the string referenced byte variable a obviously Okay, let's see. See what else. We can also use some predefined Python methods to turn a string from uppercase to lowercase or vice versa. If we want that, this can be accomplished by using the lower and upper methods.

So returning to our string, let's perform a dot lower. Now we can see our string in lowercase only great. Similarly, let's do a dot upper. And now all letters have been converted to uppercase. Sweet. keep one thing in mind here.

Although we have just applied the lower and upper methods, the initial string is still the same, no changes have been applied. So let's check a and indeed That is correct. Furthermore, this is a great proof that strings are indeed immutable. You can also verify that a string starts or ends with a particular character or substring. We have two methods available for this. They are called starts with and as you might have already guessed, ends with Let's see them in action.

So a dot starts with C returns true, that's because it is true C is the first character of the string. Now let's perform another check a dot. And with, let's say, age. This returns through as well based on the same logic because H is the last character in our string. On the other hand, let's also try a dot ends with a Q. And of course, this returns false since our string ends in age not Q, three important methods which you should keep in mind because you will use them a lot when working with strings are strip split and join.

Let's test them one by one. First, the strip methods eliminates all white spaces from the start and the end of a string. So let's say we have a new string, one with three spaces before Cisco and four spaces after switch now Let's apply the strip method on string B and see the results. So b dot strip returns Cisco switch without any spaces at the beginning and at the end of the string that's nice and may be useful. So keep that in mind. Now consider that instead of three spaces on each side of the string, we had $3 signs that we want to remove.

So from a string that looks like this, we want to eliminate the leading and trailing dollar signs. For this, we should specify the character we want to remove in between strips parentheses. So C dot strip of the dollar sign will indeed return a nice and clean string. But what if we want all spaces removed from the string including those inside the string, then we would use the Replace method instead of strip. Let's return to the string referenced by variable b, which has basis at the beginning inside and at the end of the string. You can use replace like this to get the string clean.

So b dot replace. This way you are actually replacing every space character, the first character in between parentheses with a so called empty string, the second character in between parentheses, it works, the result is the one we expected. So great job again. Now let's have a look at the split method. As its name implies, this method simply split a string into sub strings. Furthermore, you can specify a delimiter for splitting the string.

The result of this method is the list. Don't worry, you will learn more about lists very soon. For now let's see this method in action. Let's say that we have a string referenced by variable D like this, the network device manufacturers in the string are delimited by commas, so the comma will be regarded as our delimiter for the split, what if we want to extract each provider from the string in a nice format? Well, in this case, the Split method saves the day. Let's type in the following d dot split.

And in between parentheses. In between double quotes, we enter the delimiter of the comma. Python returns a list where each provider in the string is an element of this list, and can be further used into an application. You can split by any delimiter you have in a string, and get a list of elements that were previously separated by that particular delimiter. Finally, we have the join method for dealing with strings. Let's remember string a, what if we want to insert a character in between every two characters of the string, so we want to change this string as you can see it right now to something like C underscore, I underscore s underscore C and so on.

For this we will have to use the join method in the following way. And this is a bit different than the syntax we've seen so far with the previous methods. First, you type in the character you want to use as a separator or delimiter enclosed by double quotes. So in our case, this would be the underscore character. Of course, you can use single quotes instead of double quotes as well. Next, you type in a dot, then join, and in between its parentheses, the name of the variable a.

And now, you can see that the result we were looking for has been achieved. By the way, this could be read like use the underscore separator to join the elements of string a, this is a translation in plain English. Please find the comprehensive list of string methods in the link I have attached to this lecture. Since there is a huge number of such methods and we cannot cover all of them inside this course. However, you have seen the most useful and the most frequently used ones in this lecture.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.