declare

Python Strings || Python Tutorial || Python Programming



Sharing buttons:

when programming text is represented by

strings in Python there are many ways to

create a string after all there are many

different types of text

some have apostrophes some have

quotation marks and some long strings

have new lines your goal today make

strings in three different ways to begin

open a terminal and start Python first

we create a string and store it in a

variable

this stores the string meet me tonight

in a variable called message you do not

have to type a semicolon at the end like

you do in many other languages like Java

or C++ you can see the strings stored in

message by printing it

we created our first string using

quotation marks but you can also create

strings using single quotes

let's verify that the text is stored in

this variable by printing it

so you can make a string using single

quotes or double quotes we didn't have

to specify that message and message two

were strings we just assign them strings

and the Python interpreter was smart

enough to know that the variables should

hold strings but what's the point of

this to have more than one way to make a

string well suppose you wanted to make a

string and one of the words had an

apostrophe if you try to make the string

using single quotes you get a syntax

error

do you see the problem when the Python

interpreter encountered the apostrophe

in the word I'm it thought that that was

the end of the string so the remaining

text caused the error there are two ways

to fix this one way is to escape the

apostrophe by putting a backslash in

front of it this tells python that the

single quote is to be treated as a

single character and not the end of the

string but the savvy way is to create

the string using quotes

no errors no escape characters if you

make a string using double quotes but

your text contains a quotation mark you

get another error this is because Python

interprets the quotation mark before

beam as the end of the string we can

avoid this error by using single quotes

to make the string

but how do you make more complicated

strings strings which contain

apostrophes and quotation marks for this

case you can begin and end the string

using triple quotes you can use three

double quotes or three single quotes

will use double quotes

this text has single quotes double

quotes and even new lines

did you notice the triple dots which

appeared while typing this that's how

Python tells you the command you're

typing is taking more than one line so

you can create strings in Python using

single quotes double quotes or triple

quotes this makes it easy to store all

kinds of text without having to resort

to trickery and you can quote me on that

[Music]