1
00:00:00,000 --> 00:00:04,990
The string object in Python supports a lot of standard methods.

2
00:00:05,000 --> 00:00:06,990
We're going to take a look at just some of them here.

3
00:00:07,000 --> 00:00:09,990
We'll start with a string. I'm going to go ahead and put it in a variable and

4
00:00:10,000 --> 00:00:12,990
say s = 'this is a string'.

5
00:00:13,000 --> 00:00:16,990
And notice that's all lowercase, and we just look at the string by itself.

6
00:00:17,000 --> 00:00:17,990
It looks like that.

7
00:00:18,000 --> 00:00:23,990
If I say capitalize(), then we get it with just the first letter capitalized.

8
00:00:24,000 --> 00:00:27,990
I want all the letters capitalized I can say .upper() and there is certainly a

9
00:00:28,000 --> 00:00:29,990
lot of use for that.

10
00:00:30,000 --> 00:00:35,990
Likewise, I can say .lower(). Of course it was all lowercase before, so I had

11
00:00:36,000 --> 00:00:41,990
'THIS IS A STRING' in all uppercase I can say .lower().

12
00:00:42,000 --> 00:00:43,990
And we'll see that we get the lowercase version of that.

13
00:00:44,000 --> 00:00:46,990
We can also say swapcase().

14
00:00:47,000 --> 00:00:54,990
So if I had 'This Is A String,' sort of title case-ish, I say .swapcase().

15
00:00:55,000 --> 00:00:57,990
Then I get it in the opposite case.

16
00:00:58,000 --> 00:01:02,990
If I take our string, which says, 'this is a string' and I want to find a

17
00:01:03,000 --> 00:01:07,990
particular word in it, I can look for it say the word "is."

18
00:01:08,000 --> 00:01:09,990
And we will find it at position 2.

19
00:01:10,000 --> 00:01:15,990
And you might look here and you might say, well, "is" is at position 0, 1, 2, 3,

20
00:01:16,000 --> 00:01:20,990
4, 5, but in fact, it found this.

21
00:01:21,000 --> 00:01:22,990
So it's looking for exactly that string.

22
00:01:23,000 --> 00:01:25,990
In the first occurrence of it is the one that get will return.

23
00:01:26,000 --> 00:01:31,990
And so the first letter is 0, the second letter is 1, and then "is" starts at position 2.

24
00:01:32,000 --> 00:01:33,990
We can also do string replace.

25
00:01:34,000 --> 00:01:39,990
I can say s.replace, this for that.

26
00:01:40,000 --> 00:01:41,990
And it'll say 'that is a string'.

27
00:01:42,000 --> 00:01:44,990
Now, it's important to note that the string itself is immutable.

28
00:01:45,000 --> 00:01:48,990
So the string has not changed with these functions do as they return a different string.

29
00:01:49,000 --> 00:01:52,990
And it's an entirely different string object that gets returned.

30
00:01:53,000 --> 00:01:59,990
For example, if I look at the id of (s) I'll see that its id is that number there.

31
00:02:00,000 --> 00:02:08,990
And if I say newstring = s.upper(), I now have a new string which is

32
00:02:09,000 --> 00:02:10,990
the uppercase string.

33
00:02:11,000 --> 00:02:14,990
It's that different string that was returned by the upper() method.

34
00:02:15,000 --> 00:02:18,990
If I look at the id of (newstring), you'll see that it's entirely different than the

35
00:02:19,000 --> 00:02:20,990
id of 'this is a string'.

36
00:02:21,000 --> 00:02:25,990
So there's few more useful ones, there is a strip. I say s.strip().

37
00:02:26,000 --> 00:02:29,990
What strip does is that strips a particular sequence of characters from both

38
00:02:30,000 --> 00:02:32,990
the end of the string and the beginning of the string.

39
00:02:33,000 --> 00:02:34,990
By default what it strips is whitespace.

40
00:02:35,000 --> 00:02:39,990
So if I have a string that says ' this is a string ' like that, with a bunch of

41
00:02:40,000 --> 00:02:40,990
whitespace at the beginning and the end of it,

42
00:02:41,000 --> 00:02:42,990
and I say .strip(),

43
00:02:43,000 --> 00:02:46,990
I'll get back the string without that whitespace at the beginning and the ending of that.

44
00:02:47,000 --> 00:02:51,990
And so where this very commonly used for is removing new lines, but you need to

45
00:02:52,000 --> 00:02:57,990
be aware when you use it for that, that it will also remove any leading space.

46
00:02:58,000 --> 00:03:09,990
If you want to just remove whitespace from the end of string, you can use rstrip().

47
00:03:10,000 --> 00:03:13,990
And that will remove the whitespace from the end of a string and not change

48
00:03:14,000 --> 00:03:14,990
anything at the beginning of the string.

49
00:03:15,000 --> 00:03:18,990
And if you just want to remove a new line, let's say we have a string, I'm going

50
00:03:19,000 --> 00:03:23,990
to put this in a variable because we need it a couple of times, and say s1 =

51
00:03:24,000 --> 00:03:27,990
'This is a string\n' with the newline at the end of it.

52
00:03:28,000 --> 00:03:30,990
I said sting instead of string. That will work.

53
00:03:31,000 --> 00:03:34,990
And there we see we have the string with a newline at the end of it.

54
00:03:35,000 --> 00:03:39,990
If I say s1.strip(), of course, it'll remove the new line.

55
00:03:40,000 --> 00:03:41,990
And that's very useful, but it will also remove any whitespace from the

56
00:03:42,000 --> 00:03:42,990
beginning or the end.

57
00:03:43,000 --> 00:03:46,990
If I just want to remove the new line from the end, I can actually say

58
00:03:47,000 --> 00:03:51,990
s1.rstrip and specify the newline.

59
00:03:52,000 --> 00:03:55,990
So whatever string you specify, parenthesis is what will be stripped.

60
00:03:56,000 --> 00:04:00,990
And there we have it with just a newline taken from just the end of the string.

61
00:04:01,000 --> 00:04:05,990
There's also a set of methods for testing the content of a string really quickly.

62
00:04:06,000 --> 00:04:09,990
We'll look at these. We can say s.isalnum().

63
00:04:10,000 --> 00:04:12,990
And what that is, is that it checks if the string has only

64
00:04:13,000 --> 00:04:13,990
alphanumeric characters in it.

65
00:04:14,000 --> 00:04:16,990
And of course, this is False because it also had spaces in it.

66
00:04:17,000 --> 00:04:22,990
If I have a string that looks like 'thisisastring' and has no spaces in it and I

67
00:04:23,000 --> 00:04:26,990
say .alnum() and it'll come up True.

68
00:04:27,000 --> 00:04:35,990
I must have spelled something wrong. I did. 'thisisastring'.isalnum() and

69
00:04:36,000 --> 00:04:36,990
that comes up True.

70
00:04:37,000 --> 00:04:40,990
So these methods that start with "is," there is a bunch of them. I'm just going to

71
00:04:41,000 --> 00:04:46,990
show you a few. There's isalpha(), 'thisisastring'.isalpha().

72
00:04:47,000 --> 00:04:49,990
And that checks just for alpha characters.

73
00:04:50,000 --> 00:04:55,990
So that would be the letters a-z or other alpha characters in other text

74
00:04:56,000 --> 00:04:57,990
encodings beside just ASCII.

75
00:04:58,000 --> 00:05:00,990
And there is also one for checking if it is digit.

76
00:05:01,000 --> 00:05:05,990
And so if I have a string that's just digits and say isdigit(), that one will

77
00:05:06,000 --> 00:05:11,990
come up True, but if I check s.isdigit() it will of course come up False.

78
00:05:12,000 --> 00:05:17,990
And finally there's isprintable(), which checks to see if all the characters in

79
00:05:18,000 --> 00:05:19,990
the string are printable.

80
00:05:20,000 --> 00:05:22,990
So those are some of most common and most popular methods that are available

81
00:05:23,000 --> 00:05:33,000
for the string class.

