FactorPad
Build a Better Process

The First Six Python Text Functions for Strings

Here we practice with three text functions covered earlier and introduce three more.
  1. Find help - Review how to find help on your version of Python.
  2. Built-ins - Find a list of 68 functions at two sources.
  3. Review 3 - Practice with print(), type() and str().
  4. New 3 - Try out len(), ord() and chr() functions.
  5. Next: sequences - Introduce escape sequences.
face pic by Paul Alan Davis, CFA
Updated: February 21, 2021
You will find an organized plan for learning nearly all built-in functions. Keep reading and follow along in your version of Python.

Outline Back Tip Next

/ factorpad.com / tech / full-stack / python-text-functions.html


An ad-free and cookie-free website.


Learn new text functions for strings in Python 3

Beginner

A beginner Python tutorial on text functions for strings in Python with an introduction to character encoding.

Video Tutorial

Videos can also be accessed from our Full Stack Playlist 3 on YouTube.

Text functions for strings in Python for beginners (4:44)

Code Examples and Video Script

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)

Step 1 - Review How to Find Python Help

Let's head to the Linux Terminal, and grab the version number of Python.

paul@fullstack:~$ python3 -V Python 3.4.2
Locate help on python.org

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.

Step 2 - Find a List of 68 Functions at Two Sources

Built-in functions at python.org

Here they are, all 68 of them, and here I'll introduce len(), ord(), chr(). Clicking here (the chr function) gives us a description.

Built-in functions in a text document

Okay, so I put the built-in functions in a text document viewed using the Linux pager less.

paul@fullstack:~$ less notes/python_builtins.txt

Noted is the video (tutorial) number where I introduced the function.

Python builtins - functions built-in to Python version 3.4.2 abs() dict() help() (28) min() setattr() all() dir() hex() next() slice() any() divmod() id() object() sorted() ascii() enumerate() input() oct() staticmethod() bin() eval() int() (27) open() str() (26) bool() exec() isinstance() ord() (30) sum() bytearray() filter() issubclass() pow() super() bytes() float() (27) iter() print() (26) tuple() callable() format() len() (30) property() type() (26) chr() (30) frozenset() list() range() vars() classmethod() getattr() locals() repr() zip() compile() globals() map() reversed() __import__() complex() hasattr() max() round() (27) delattr() hash() memoryview() set() Note: built-ins are often called builtins notes/python_builtins.txt (END)

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.

Step 3 - Practice with print(), type() and str()

Let's start with the first function most people learn, print().

paul@fullstack:~$ python3
The Python print() function

It can take integers, or text strings surrounded by single-quotes, or text strings surrounded by double-quotes.

>>> print(30) 30 >>> print('text string') text string

It can also take variables like x, but as noted (earlier), we have to define them first.

>>> print(x) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'true' is not defined >>> x = 'text string' >>> print(x) text string
The Python type() function

Second, the type() function identifies the data type of an object, like x, and this is a string.

>>> type(x) <class 'str'>

Numeric types are integers and floats, if you recall.

>>> type(30) <class 'int'> >>> type(30.0) <class 'float'>
The Python str() function

Third, we can convert what Python automatically interprets as an integer, viewed with the type() function.

See 30 is an integer (and 30.0 is a float).

>>> y = 30 >>> type(y) <class 'int'>

We can assign that value of 30 as a string to a new variable z, like this. Confirming it with type().

>>> z = str(y) >>> type(z) <class 'str'> >>> print('This is video #' + z) This is video #30

Even put it in a sentence using print(), concatenating with the plus and the variable z.

Step 4 - Try out len(), ord() and chr() functions

The Python len() function

Let's move on to the len() function 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.

>>> len('count me') 8

Or bringing back x, we can throw that right in to the len() function as well.

>>> x 'text string' >>> len(x) 11

And remember y was 30 as an integer.

>>> y 30

We can't len() and integer.

>>> len(y) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: object of type 'int' has no len()

But z as the text string of 30 works, and it has 2 characters.

>>> z '30' >>> len(z) 2
The Python ord() function

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.

>>> ord('a') 97 >>> ord('A') 65
The Python chr() function

Going the other way, we can input that integer into the chr() function to find the character.

>>> chr(97) 'a'

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.

Step 5 - Next: Text functions in Python

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.


What's Next?

I hope you're enjoying the ad-free content. Subscribe and follow to stay connected.

Outline Back Tip Next

/ factorpad.com / tech / full-stack / python-text-functions.html


python text
python string functions
python string
python len
python ord
python chr
python ascii
python unicode
python character encoding
python text functions
python builtin functions
python data science
beginner python
learn python
python tutorial
python type
python print

A newly-updated free resource. Connect and refer a friend today.