{article Learn Python The Hard Way}{text}{/article}

Strings are amongst the most popular types in Python. We can create them simply by enclosing characters in quotes. Python treats single quotes the same as double quotes.

Creating strings is as simple as assigning a value to a variable.

Python Strings:

Strings in Python are identified as a contiguous set of characters in between quotation marks. Python allows for either pairs of single or double quotes

tutorialspoint Simply Easy Learning

Strings and Text. Exercise 6:

#ex6.py Notepad ++

x = "There are %d types of people." % 10
binary = "binary"
do_not = "don't"
y = "Those who know %s and those who %s." % (binary, do_not)

print x
print y

print "I said: %r" % x
print "I also said: '%s'. " % y

hilarious = False
joke_evaluation = "Isn't that a joke so funny?! %r"

print joke_evaluation % hilarious

w = "This is the left side of..."
e = "a string with a right side."

print w + e

Window PowerShell

PS C:\mystuff> python ex6.py
There are 10 types of people.
Those who know binary and those who don't.
I said: 'There are 10 types of people.'
I also said: 'Those who know binary and those who don't.'.
Isn't that a joke so funny?! False
This is the left side of...a string with a right side.

Study Drill 1:

Go through this program and write a comment above each line explaining it.

#%d is the formatting character, inserting 10 into the string, resulting in the string::

#There are 10 types of people.
x = "There are %d types of people." % 10

#Assigns the string "binary" on the right to a variable binary on the left.
binary = "binary"

#Assigns the string "don't" on the right to a variable do_not on the left.
do_not = "don't"

#%s is the formatting character, inserting binary and don't into the string, resulting in the string::

#Those who know binary and those who don't.
y = "Those who know %s and those who %s." % (binary, do_not)

# prints: There are 10 types of people
print x

# prints: Those who know binary and those who don't.
print y

#%r is the formatting character, inserting variable x into the string, resulting in the string::

#There are 10 types of people.
print "I said: %r" % x

#%s is the formatting character, inserting variable y into the string, resulting in the string::

#I also said: 'Those who know binary and those who don't.
print "I also said: '%s'. " % y


#Assigns the string False on the right to a variable hilarious on the left.
hilarious = False

#Assigns the string "Isn't that a joke so funny?! %r"" on the right to a variable joke_evaluation on the left.
joke_evaluation = "Isn't that a joke so funny?! %r"

# prints: Isn't that a joke so funny?! False
print joke_evaluation % hilarious

#Assigns the string "This is the left side of..." on the right to a variable w on the left.
w = "This is the left side of..."

#Assigns the string "a string with a right side." on the right to a variable e on the left.
e = "a string with a right side."


# Concatenates two strings w + e puts one after the other.
print w + e

Study Drill 2:

Find all the places where a string is put inside a string. There are four places.

# 2 strings inside a string total 2

y = "Those who know %s and those who %s." % (binary, do_not)

# 1 string inside a string total 3

print "I said: %r." % x

# 1 string inside a string total 4
print "I also said: '%s'." % y

Study Drill 3:

Are you sure there are only four places? Yes

How do you know? A string is usually a bit of text you want to display to someone, or "export" out of the program you are writing. Python knows you want something to be a string when you put either " (double-quotes) or ' (single-quotes) around the text. Strings may contain the format characters you have discovered so far. You simply put the formatted variables in the string, and then a % (percent) character, followed by the variable.

Maybe I like lying. Maybe not.

Study Drill 4:

Explain why adding the two strings w and e with + makes a longer string.

Concatenates two strings w + e puts one after the other.