Build a Better Process

Python Strings and Python Variables: 9 Rules to Remember

Getting a platform started for Data Science is often a tricky proposition, but with that behind us we are ready to start programming.
  1. Our first program - Create our first, yet basic, program.
  2. How we got here - See a summary of our hardware and software setup.
  3. Variables - Learn the 7 rules for variables right in the Python Interpreter.
  4. Text strings - See the 2 rules for text strings.
  5. Next: functions - Introduce Python functions.
face pic by Paul Alan Davis, CFA
Updated: February 21, 2021
These lessons will stick if you type each example in your Python Interpreter.

Outline Back Tip Next

/ factorpad.com / tech / full-stack / python-strings.html

An ad-free and cookie-free website.

Learn the rules that apply to Python strings and variables


Video Tutorial

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

Python strings and Python variables: 9 rules to remember | Python for Beginners (4:43)

Code Examples and Video Script

Welcome. Today's question: How do you create text strings and variables in Python?

I'm Paul, and as we near 2 hours runtime in this Series, it's okay if you're frustrated we have yet to write a program. Trust me, I am too.

So in this video we'll get one under our belt, even though it's ultra-basic and just done at the Python Terminal.

I'll zip through how we got here, then cover 7 guidelines for creating variables and 2 for creating text strings.

(Commands in Linux)

(Symbols and functions in Python)

The next tutorial is on functions, like this funny looking print() guy here, which is, in fact, the focus of our very first program here.

Step 1 - Our First Program

So while I show you where we're headed and our planned software stack, I want to say that the frustration of starting slow is normal. In Data Science, for example, there are lots of hurdles in getting smart minded people to ditch their spreadsheet and learn real Data Science in a programming language.

There's the soft stuff like figuring out which books to buy, what classes to take, websites to visit and which YouTube resources to trust, which can be a headache all by itself.

Step 2 - How We Got Here

Then there is the technical stuff, I'll poke around on this Linux server, highlighting our path with technical decisions like selecting the appropriate hardware, the operating system, setting up users and groups, which software languages to learn, selecting which versions to focus on, and the whole installation process, often a major stumbling block.

paul@fullstack:~$ free -h total used free shared buffers cached Mem: 7.7G 765M 7.0G 160M 146M 457M -/+ buffers/cache: 161M 7.6G Swap: 7.9G 0B 7.9G paul@fullstack:~$ lsb_release -a No LSB modules are available. Distributor ID: Debian Description: Debian GNU/Linux 8.6 (jessie) Release: 8.6 Codename: jessie paul@fullstack:~$ id uid=1000(paul) gid=1000(paul) groups=1000(paul),24(cdrom),25(floppy),27(sudo),29(audio),30(dip) ,44(video),46(plugdev),108(netdev) paul@fullstack:~$ python -V; python3 -V Python 2.7.9 Python 3.4.2 paul@fullstack:~$ clear paul@fullstack:~$ python3

That's what we hit in the first 22 tutorials, which all leads us to here, our first program.

Step 3 - The 7 Rules for Python Variables

I'll bet that you've seen this before, >>> print("Hello World!"), maybe 90% of instructors start with it, but I'm not a conformist, so this is how I'll start, with a two-liner.

paul@fullstack:~$ python3 Python 3.4.2 (default, Oct 8 2014, 10:45:20) [GCC 4.9.1] on linux "help", "copyright", "credits" or "license" for more information. >>> first_name = "Paul" >>> print("My name is " + first_name) My name is Paul

I suggest following along in your Python, preferably version 3, because some syntax is different, including this print() function.

Let's break down what happened there covering seven things you should know about variables.

Rule 1 - Assign a value to a variable with one equals symbol

First, the variable name here, first_name, is on the left, and whatever is on the right of the single equal symbol is assigned to it.

Here we assign a string of text. After Enter Python stores it in memory.

Rule 2 - Conventions for variable assignment

Second, what's the best form to use? We could be lazy and do y=2 with no spaces.

>>> y=2 >>> y 2

Or for better form, uses spaces like this y = 2.

>>> y = 2 >>> y 2

Here we assigned an integer to the variable y, printing it if we don't need to fancy it up with text.

Rule 3 - Standards for variable names

Third, variable names should explain what they contain, like bucket names.

>>> first_name = "Paul"
Rule 4 - Reassigning values to variables

Fourth, once you reassign the variable, the old value is forgotten (y was assigned 2 above).

>>> y = 3 >>> y 3
Rule 5 - Variable names must start with a letter or underscore

Fifth, variables must start with a letter or underscore, and that's it. Avoid all of the other characters, but numbers are okay afterwards.

>>> y2 = 5 >>> y + y2 8

So y2 works, and so do math expressions, like y + y2.

Rule 6 - Variables must be one word

Sixth, variables need to be one word, no spaces (or Python gives you an error).

>>> y 3 = 2 File "<stdin>", line 1 y 3 = 2 ^ SyntaxError: invalid syntax. >>>
Rule 7 - Variables are case sensitive

Seventh, variables are case sensitive, so uppercase First_Name and lowercase first_name are not the same.

>>> First_Name = "Jennifer" >>> First_Name 'Jennifer' >>> first_name 'Paul'

It's good form to start with lowercase letters.

While it's on my mind, to brush up, or learn those Linux commands from earlier, go here, Linux Essentials, and you'll find one hundred 3-4 minute command summaries and quizzes that sit in our Linux Playlist.

Step 4 - The 2 Rules for Python Strings

Let's talk about text strings.

Rule 1 - Use single or double quotation marks

First, they can be entered with single or double quotation marks.

Two choices are available because you may need to use an apostrophe ' or quote " in your string, and surround it with the other, or you'll get an error.

>>> response = "How's life?" >>> response = 'How's life?' File "<stdin>", line 1 response = 'How's life?' ^ SyntaxError: invalid syntax. >>>

Python assumed the string ended here (with the second ' in 'How').

Rule 2 - Connect text strings with concatenation using plus

Second, concatenation means adding text strings together with a plus symbol +, so let's add to the original.

>>> print("My name is " + first_name + ". " + response) My name is Paul. How's life? >>> exit()

print() is the name of the function and it prints what's passed to it, inside parentheses, called an argument.

Step 5 - Next: Python Functions

So here is the stack we are adding to, one tutorial at a time.

In the next tutorial we will explore functions.

Have a nice day.

What's Next?

See more free learning at our YouTube Channel. Subscribe for reminders.

Outline Back Tip Next

/ factorpad.com / tech / full-stack / python-strings.html

python strings
python variables
python print
python string concatenation
python strings rules
python variables rules
python concat string
learn python online
learn python
python naming conventions
python lowercase
python variable name rules
rules of python strings
python best practices
python tutorial

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