Variables, Data Types, and String Manipulation in Python

Explain Python variables, data types, string concatenation, indexing, and slicing, and demonstrate how to avoid nested quote conflicts.

Unlock the power of Python variables by learning essential naming rules, data types, and best practices. Quickly grasp core concepts like concatenation, data types, and boolean logic to streamline your coding skills.

Key Insights

  • Understand that Python variables store different data types, including integers (whole numbers), floats (decimals), strings (text), and booleans (true or false). Each variable must follow specific rules, such as not beginning with a number, avoiding reserved words like "print," and excluding spaces or special characters except underscores.
  • Embrace best practices in Python programming, such as adopting "snake case" naming conventions (using lowercase letters and underscores to separate words), using meaningful yet concise variable names, and capitalizing all letters for constant variables that remain unchanged, like tax rates.
  • Explore key Python functionalities including concatenation (combining strings with the plus sign), using built-in methods like type() to identify variable data types, and applying indexing to access specific characters within strings (starting with position zero), enabling precise text manipulation.

Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.

This is a lesson preview only. For the full lesson, purchase the course here.

So let me just slide back up a moment. We talked about the formatting, but let's talk about what it's saying here. A variable is a named holder of a value.

So you don't have to be a Programmer to know that. If you have ever typed into a form—enter your first name, enter your email, enter your username and password—you're providing values for variables. If you took math in high school—X equals five, and you have an X-Y coordinate system in geometry—those X's and Y's are variables, and those numbers are their values.

When you first learn about variables in math class, you're typically dealing with numbers, be they ints or decimals, and Python distinguishes between ints and decimals. Int is a data type for an integer—negative, positive, or zero whole number—whereas float is the data type for a decimal. So 3.5 would be a float.

String is any kind of text—text in quotes. So your first name that you're typing into a form field or even into here as a variable—these are string variables. So it says string, the data type abbreviated as str or str.

We've got these numbered steps. Let's declare a few string variables and assign with equal. What that means is we are declaring the variable, calling it first name, and then we have an equal sign, and then we have a value in quotes because it's a string.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

String values go in quotes—single or double are both okay. And what's happening is the value on the right is being assigned to the variable on the left. So it's kind of like going that way.

So first name equals Brian, last name equals McLean. Now let's talk real quick about variable naming rules and conventions. So rules must be followed or your code will break, and conventions are just best practices.

You can ignore a best practice and your code won't break, but it might be frowned upon by other Developers or just something that's not popular in the Python world. So you cannot have any spaces in variable names. No special characters or punctuation—only letters, digits, and underscores.

You cannot start with a number. So you cannot have a variable called one_day, but you could have a variable called day_one. You could also have a variable—if you really want to start with a 1—you can kind of cheat by using an underscore.

You'll see that a lot. If you start with an underscore, you're not starting with a number, right? As long as you don't start with a number, you're good. So you could have variables.

You'll see those starting with an underscore. You cannot use reserved words. So there are all kinds of words in Python.

Let's say you want to print something—there’s a print function. So you really can't make a variable called print or str or len, which means length, or input, which is a function for letting you type into an input box, or type, which tells you the data type.

It’s a function that takes a variable and tells you the data type. All kinds of reserved words in Python—off-limits. In other words, do not declare a variable called print. Don’t say print equals whatever and have that be a variable, because it'll overwrite the print method—which we're going to look at now.

So let's say we want to print our names. We'll say print first name, and we could also print—let's print the last name. And if you want to print both on the same line, you can do that.

Just separate the variables with a comma and it should work just fine. Actually, we do have to run this cell so that it knows these variables exist. By running the cell, we're logging the variables into memory. If you get an error that says "first name is not defined, " it's because you hadn't run the cell where we declared the variables.

So it didn't know about first name or last name. Now it knows, and it works. That looks like it's been combined into one string—the full name here—but it really hasn't.

It just printed two separate things. If you want to truly combine substrings into bigger strings, you can do so with the plus sign. The plus sign does double duty.

It's obviously used for addition, but it's also used for combining strings. That process is known as concatenation. But first, another word about variables.

Well, we talked about the rules. If you do something that’s not a good idea—like setting a variable equal to print and thereby breaking your print function—you can get a fresh start by going up to "Runtime" → "Restart session and run all." If things are just not working and you don’t see any errors, but it’s still broken…

It’s not printing correctly. You might have some bad variables stored in memory, and you just need to restart the session and run all. If you mess up by using a reserved word, for example—restart the session.

It's important. Without that, you can really spin your wheels. Like, "What do I do? There are no errors, and it’s still not working."

So know that one. That’s why I’m giving it to you right off the bat. Conventions—okay—as opposed to rules, they’re best practices.

You can break them, but it’s considered frowned upon. We’re going to use what’s called snake_case or PascalCase for our variable naming. In other words, we connect the separate words here with an underscore because you can’t have spaces.

Whereas in JavaScript, they wouldn’t do that. They don’t use the underscore. You could.

It’s not going to break your code, but in JavaScript, the convention—the cultural norm, so to speak—is to use what is called camelCase, where you just put the words together and capitalize any second or third word. Another convention is to keep your variable names short yet meaningful. So you want to kind of thread the needle.

You don’t want them too short because then people don’t know what they mean at all, but you don’t want them really long either. So employee telephone number might be a little long, whereas emp_tel would be better. Like, people kind of get that employee is emp, but you wouldn’t want to call it just tel because it’s not specific enough.

I mean, we know it’s a telephone, but we don’t know—Is it the company telephone? Is it the customer’s telephone? So you might want to just be a little specific with abbreviations that are well understood and stay away from really short variable names.

You wouldn’t want to call it "t, " right? Because that could be anything. You want to go lowercase in your variable names—you don’t capitalize them.

So first_name the way we wrote it—not capital F. And if you ever want to do a value that never changes—like it can't change during the course of the program—the Programmer is not going to change it. It’s going to stay the same the whole time. That would be what is known as a constant.

And the convention for constants is to capitalize every single letter—uppercase the entire constant. And that is sort of a flag to other Programmers not to change this. New York State sales tax, for instance. If you’ve got a little app that calculates a restaurant bill based on food and beverage—which we’re going to make in this file—and tip, all those values are variables.

The food and beverage amounts will change every time. The tip will change. But the sales tax rate of 8.875% will not change in New York City. It would be the same for every bill. So that kind of constant value would be assigned to a variable that is all uppercase.

So a constant variable—to use an oxymoron.

All right, now back to concatenation. So concatenation is when you use a plus sign to combine multiple substrings—as they're called—or strings into bigger strings.

So let's try that. We're going to make a variable called `full_name`, and we'll say `full_name = first_name + last_name`.

And then we'll print the full name and see what we get. And it does work, although it looks kind of like a username because there's no space. It's really just two variables crammed together.

If you want a space, you actually have to type it and connect it to the next variable with another plus sign. A little tedious. There is a workaround—a better way—that we’ll learn about later.

But for now, that's concatenation: plus signs between each item, and the items typically alternate between substrings and variables—like substring, variable, substring—or vice versa.

Okay, so right now, we're going to do a little challenge.

I'd like you to pause and try to CONCATENATE a string called `name_tag`, like so. You're going to declare a variable called `name_tag` and set its value equal to: “Hello, my name is Brian McLean” (or your name, of course). You can use `full_name`, or you can use `first_name` and `last_name` separately.

So pause and go ahead and try to get that to work. And then you want to print the `name_tag` so we know it worked. Right, I'll give you a start.

Look—“Hello.” Right, the beginning of every name tag. The cornerstone of every nutritious breakfast.

All right, there you go. `name_tag = "Hello…"` Pause.

Try the challenge. Okay, we're back with the solution to the 01 challenge. We're going to say `name_tag = "My name is " + first_name + " " + last_name`.

Of course, we could have just used `full_name` here, but I won’t. And then, if you want another exclamation point or a period, you can go ahead and CONCATENATE that on—punctuation—and run it. And we are getting an error because I put an equal sign here.

It needs to be a plus sign. There you go. That worked.

Another exclamation point—concatenation. All right, next up—the `type` operator. Actually, the `type` method—I should say. It’s not an operator.

It’s called a function. It’s got parentheses after it and takes a value. That is known as a function or a method. Like `print` is—it takes a value.

Its job is to print it. `type` takes a value—a variable. Its job is to tell you the data type of the variable.

So you feed a variable into the `type()` method, and it's going to return—or give you—the data type. And you can directly print that operation. So let's print.

Let's come into this next cell here. So what we're doing here is this is a function, and it takes an argument. The input of a function is the argument.

The answer is known as the return value. So in this case, `type` returns the data type of the variable you pass to it, with—again—the return value being the “answer.” So because `type` returns a value, we're just going to feed that value directly into `print()` and run—and there you go.

It is a string. Notice it’s not printing the `name_tag` because we're not telling it to print the `name_tag`. We already did that up here.

It's printing the data type of the `name_tag`, which is a string. All right, let's print a few more. Let's just get a sample going of the various data types.

`type(85)` is an int, but `type(8.5)` is a float. And then you can also look at what is called a Boolean. A Boolean is a value—

Let’s come back here for a moment to our variables.

A string is text in quotes. An integer is a whole number. A float is a decimal. A Boolean is a value that is only ever true or false. So, you know—a lot of these terms like Boolean—don’t get psyched out by the terminology.

It just means true or false. Yes or no, right? The fridge is open or not. The house is locked or not.

True or false—the cat is lying in the windowsill or not. So just true or false. Lots of stuff.

In applications, there are all kinds of switching on and off: user is logged in or not logged in, the cursor is moused over here or it's not.

All kinds of detections for Booleans. A number has a remainder when you divide it by two—or not. We’re going to get into that.

The use of a Boolean is very important when we get to what’s called conditional logic—or branching with if statements. But for right now, let’s just look at the pure idea of a Boolean—something that’s true or false.

We can certainly say `True`—or type it, rather. The type of `True` is Boolean, and the type of `False` is also Boolean. You can also do a comparison.

Let’s say `3 > 5`. That’s false. So again, it is false.

But we’re not asking if it’s true or false—we’re getting the data type of that. Let’s actually find out if it’s true or false first.

So, `print(3 > 5)`—that’s false. `print(3 < 5)`—that would be true. We’re getting Boolean values from comparisons, and we’re also getting the data type of those comparisons—Boolean. And we’re going to look in a moment at another one.

There’s a `len()` function. If you pass a string to the `len()` function, it tells you how many letters there are. That’s what it returns.

So, the `len()` of "apple" is five, right? If you print that, it'll give you 5. And that being 5, you could check to see if that’s greater than 4—and it’s true, right? Apple: the length is 5, and 5 > 4.

So here we have a function that returns a number, being compared to another number, with the comparative result returning a Boolean: true or false. They didn’t teach you that in third grade when they introduced greater than and less than operators to you.

They didn’t even call them operators, right? The greater-than sign, the less-than sign.

They didn’t tell you that it returns a Boolean. All they told you was: it’s true or false.

All right, moving on—but pause and type, everybody, please.

I mean, that’s what—again—you’re going to learn a million times better if you type this. It’ll take longer, obviously, but it’s worth it. This is a very long file, by the way.

You’ll want to probably do it in chunks. Get up, walk around. You don’t really want to sit and do this more than an hour or two at a time without stretching—probably not even that long. Walk around, stretch.

I say that for myself as much as for you. I’ll sit for hours. It’s not good.

If you’re going to be doing this for the long haul—play the long game. You’re going to want to take care of your body: get up, move around, give your eyes a break—as much as your hands—and just get the circulation going. So anyway—yeah—take breaks whenever you need to. Pause. But when you’re on, I really need you to be typing for you to learn this.

Okay, `len()`—print the number of characters in `name_tag`. So `name_tag` is kind of long, right? That’s a fairly long string. Let’s find out how long it is.

How many letters? How many characters? It’s got 32. Okay. And your name may have a different number of letters than mine.

Hence, you’ll get a different count—a different `len()` value. All right, let’s keep rolling. Now, strings go in quotes.

So it’s kind of tricky sometimes. What if there are quotes inside the quotes? We want to avoid a quote conflict. For example, let’s declare a song and we’ll say: “Won’t Get Fooled Again” by The Who, right? And we print the song and it works great.

No problem. But what would happen if you used single quotes? Because you’re allowed to use single quotes. In fact, here—let’s go to "apple" and put it in single quotes.

It doesn’t break anything. It works still. Single quotes or double quotes.

A lot of Programmers like single quotes. I have to hold down the Shift key—so I mix it up kind of arbitrarily.

But anyway, let’s say it’s single quotes. Now you do have an issue because the wrapper quotes in a string are meant to begin and end the string—like bookends. But if you’re switching to single quotes, Python will interpret that inner apostrophe as a single quote.

It’s the same character. So it’ll think that one is the entire string, and then the rest is just noise. And if you run the cell, it’ll throw an error: unterminated string literal.

It doesn’t even know what this is supposed to be. So what you can do is escape the nested quote by turning it off with a backslash. That’s called escaping the quote.

That tells Python, “No, no, no. This is just punctuation. It is not meant to be interpreted as the closing quote of the string.”

So just keep going. That’s one way to fix it—a nested quote conflict.

The other way is: if you’re using a single quote, just switch to double quotes—if that’s possible. It can be trickier. You can have, you know, double quotes inside single quotes with dialogue or something.

And sometimes there are other workarounds. But you just have to escape quotes. For now, that’s about all you need to know about avoiding nested quote conflicts, as they’re called.

All righty—next up. Strings have what is called an index. That means a position number for every character.

So in this case, we have this song, right? Here’s our song. It’s got some number of letters. Let’s print the length of it.

22 letters, okay? Includes spaces, characters—does not include the escape. So let’s say we want to get the first character—the "W." Whatever the first character is of a string—we want that. The way you get that is you write the name of the string—in our case, `song`—and then you put what’s called the index after it in square brackets.

The index is the numeric position, starting from zero. We’re asking for the character from position zero, which is the "W." Let’s make a variable: `first_char = song[0]`. And then we’ll save that, and we get the "W." And then let’s say `last_char`…

The last char—you won’t necessarily know how many letters there are, so you don’t want to put a 20 or 21. You just want to count backward—and that would be negative one. `song[-1]` returns the last character of the string.

Let’s change that to "returns." "Returns" just means gives you an answer or result. Returns, gets—gets and gives you.

If we have the last character—and the backslash does not count as a character. Now if you print both of these—“Won’t Get Fooled Again”—W is the first character and N is the last character.

You’d get "WN" if you print them both one after another. There you go. And the next-to-last character is -2, and the second character is 1.

And you can also select ranges, which we’ll get into. Let’s say you want from the third to the sixth character—that’s called slicing.

We’ll get into that later. We’ve got a whole bunch of content for that. Moving on to numbers.

Brian McClain

Brian is an experienced instructor, curriculum developer, and professional web developer, who in recent years has served as Director for a coding bootcamp in New York. Brian joined Noble Desktop in 2022 and is a lead instructor for HTML & CSS, JavaScript, and Python for Data Science. He also developed Noble's cutting-edge Python for AI course. Prior to that, he taught Python Data Science and Machine Learning as an Adjunct Professor of Computer Science at Westchester County College.

More articles by Brian McClain

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram