String Data Types
Previously we covered numeric data types,
int. Often times in Python, we'll have to work with text in which case we'll be dealing with
string Data Types.
The simplest definition I can come up with for a
A series of characters enclosed by either a double quote
" "or a single quote
Both of which work equally well.
name = 'John' print(name) print(type(name))
>> <class 'str'>
If I replace John with double quotes
" " it should work the exact same way:
name = "John" print(name) print(type(name))
>> <class 'str'>
When to use single
' ' quotes and double quotes
However there are some cases where one is advantageous over the other.
For example what if I wanted double quotes inside of my string.
How would I do it?
Let's try this.
dialogue = "John said "Hello James", to which which James said "Hello John""
If I try to run this, i'll get an error.
There are two solutions to this:
- Instead of writing the string with a double quote, use a single quote and you should be good to go. That way you can use double quotes inside of your string.
dialogue = 'John said "Hello James", to which James said "Hello John"' print(dialogue)
>> John said "Hello James", to which James said "Hello John"
and everything seems to work out fine.
Assume inside of that same string, I wanted to use single quotes as well.
dialogue = "John said 'Hello James', to which James said "Hello John, you're the best'" print(dialogue)
This throws an error, you can't use single quotes inside single quotes unless:
- you add the slash operator
\behind the single quote
dialogue = "John said 'Hello James', to which James said 'Hello John, you\'re the best'" print(dialogue)
>> John said 'Hello James', to which James said 'Hello John, you're the best'
This seems to work fine. If you don't put the backslash
\ python is going to think that the single quote
' is what is going to end the string. The backslash
\ helps tell Python that the single quote
' is part of the string and not ending it.
A common operation in strings is to concatenate them. We can add strings the same way we added integers and floats with the
segment_one = 'I\'m 25 ' segment_two = 'years old' full_sentence = segment_one + segment_two print(full_sentence)
>> I'm 25 years old
This operation attached both
segment_two together using the
Strings can also be multiplied. Let's multiply our full sentence by
long_sentence = full_sentence*10 print(long_sentence)
We need to concatenate these two strings to make a full sentence.
>> I'm 25 years oldI'm 25 years oldI'm 25 years oldI'm 25 years oldI'm 25 years oldI'm 25 years oldI'm 25 years oldI'm 25 years oldI'm 25 years oldI'm 25 years old
And as we expected the sentence repeats 10 times.
What if I asked you to count all the characters in that long sentence. That would take you a while.
Python has a useful built-in length
len function that returns the length of not only strings but collection types as well.
length_of_long_sentence = len(long_sentence) print(length_of_long_sentence)