Python strings have many methods available. This lesson demonstrates a few of the more common string methods.
- [Instructor] The Python string object has a number of useful methods. Here are some common ones, here in Komodo. I've opened a working copy of hello.py, from chapter 11 of the exercise files. And you notice down here on line four, we have our print, hello world. And if I run this, it prints hello world in our results. If I use the upper method on the literal string, you notice it prints in all capital letters.
And if I use the lower method, it's in all lowercase. There's also a capitalize method, which capitalizes just the first letter and makes everything else lowercase. There's a title case, which capitalizes the first letter of every word, and there's swap case, which will swap the case of every letter, so the capital letters are lowercase and the lowercase letters are uppercase. There's also casefold, and if I run this, you'll notice it makes everything lowercase.
The difference here is that casefold is similar to lower but it's more aggressive, and it removes all case distinctions, even in unicode. So if I use like a German sharp S here, and we run this, you notice that comes out to a lowercase ss. Where if I just do this in lower, and run it, you notice it does not lowercase the German sharp S. And so that's useful for words like strasse, the name of a street in German.
And if I make that casefold, then it spells it in lowercase. So it's important to note here, let's put this back to uppercase and remove that, it's important to note here that a string is immutable, it cannot be changed. So when you use one of these transformation methods, the return string is a different object. So for example, if I say s equals, and I'll just copy my string with the upper there, and s2, we'll call this one s1, and s2, equals, take this, like that, and we'll say, s1.upper.
Now I'll go ahead and I'll print the id of each of these. You'll notice that they're entirely different objects. So the upper method here, in line five, has not just transformed the string in place. Rather, it's created a new object and returned that. If you need to concatenate two strings, that is to combine them into a new string, you can use the plus operator.
So I'll just make another string here, this is another string. And I can print s1 + s2, which will concatenate them, or if I want a space in the middle, I can put a space in there like that, and run that. Literal strings can be concatenated like this.
I can print s3 and you see that that is one concatenated string. Python strings are very powerful. For more details, see the Python documentation here. This is at docs.python.org/3/library/standardtypes.html. And if we scroll down here, text sequence type, string methods, you'll see that there is quite a few of them, many more than we could cover in just a few minutes on this video.
But that's the full documentation, and you should be able to follow that pretty easily, after our introduction here.
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api
Skill Level Intermediate
Python: Programming Efficientlywith Michele Vallisneri2h 15m Intermediate
Learning Python Web Penetration Testingwith Christian Martorella2h 49m Intermediate
2. Language Overview
3. Types and Values
8. Structured Data
11. String Objects
12. File I/O
13. Built-in Functions
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.