A beginner Python tutorial on text functions for strings in Python with an introduction to character encoding.
Text functions for strings in Python for beginners (4:44)
Videos can also be accessed from our Full Stack Playlist 3 on YouTube.
Welcome. Today's question: What are the top 6 text (string) functions for Python beginners?
I'm Paul, and in my experience knowing what you don't know is a first step to learning, and there's a lot we don't know about code.
So here in this 5-minute summary, we'll review functions related to text (strings), sticking with those functions built-in to the standard Python environment.
We'll review 3 covered in video #26, then practice with another 3 before moving on to the next video (tutorial) on another related topic, called escape sequences.
(Commands in Linux)
(Functions in Python)
Let's head to the Linux Terminal, and grab the version number of Python.
If you've been with us, I pointed you to online documentation at https://www.python.org/doc/versions/, where we followed links to our specific installation of Python 3, then Library Reference, and Built-in Functions.
Here they are, all 68 of them, and here I'll introduce
chr(). Clicking here (the chr
function) gives us a description.
Okay, so I put the built-in functions in a text document viewed using the Linux pager less.
Noted is the video (tutorial) number where I introduced the function.
Remember what I said about knowing what we don't know, well this is what I meant. We only know a fraction of the builtins at this point, and later we'll load more modules and this list will grow.
Humbling, but we'll get there.
It's important to note that just like in Linux, covered in video (tutorial) #15, built-ins are oftent spelled builtins. Keep that in mind.
Let's start with the first function most people learn,
It can take integers, or text strings surrounded by single-quotes, or text strings surrounded by double-quotes.
It can also take variables like x, but as noted (earlier), we have to define them first.
type() function identifies
the data type of an object, like x, and this is a
Numeric types are integers and floats, if you recall.
Third, we can convert what Python automatically interprets as an
integer, viewed with the
See 30 is an integer (and 30.0 is a float).
We can assign that value of 30 as a string to a new
variable z, like this. Confirming it with
Even put it in a sentence using
concatenating with the plus and the variable z.
Let's move on to the
for length, as measured in characters.
Within single or double quotes you can insert a string of text and
len() will count them for you.
Or bringing back x, we can throw that right in to the
len() function as well.
And remember y was 30 as an integer.
len() and integer.
But z as the text string of 30 works, and it has 2 characters.
Now for character encoding. What do you think goes on behind the scenes
when you type an
a on your keyboard?
We'd expect it to be encoded to numbers, right?
I don't want to dig in too deeply on that topic here, but I want to
show you that the
ord() function, for
ordinal value, shows an integer for the character, so
ord() of lowercase-a is 97, and
ord() of uppercase-A is 65.
Going the other way, we can input that integer into the
chr() function to find the character.
This topic isn't typically covered in most Python beginner tutorials, but we intend to go pretty far here.
For homework, I've included links to two Wikipedia pages on character encodings.
You'll see an interesting visualization of the adoption of UTF-8 versus ASCII, from 2000 to 2010. The ASCII table layout is interesting and will be helpful for our next video (tutorial) on escape sequences, and other future topics.
For those who want to stick around, this is the software stack we're building out.
Please add a comment and subscribe so you can automatically be notified of new videos (tutorials) like the next one on escape sequences.
Have a nice day.
For reminders, subscribe straight from here.