codecademy python lesson2

字符串

2.1 Strings

A string can contain letters, numbers, and symbols.

# Set the variable brian on line 3!
brian = “hello life!”

2.2 Escaping Characters

'There's a snake in my boot!'
This code breaks because Python thinks the apostrophe in  'There's' ends the string. We can use the backslash to fix the problem, like this:

'There\'s a snake in my boot!'

2.3 Access by Index

Each character in a string is assigned a number. This number is called the index. In Python, we start counting the index from zero instead of one.

"""
The string "PYTHON" has six characters,
numbered 0 to 5, as shown below:

+---+---+---+---+---+---+
| P | Y | T | H | O | N |
+---+---+---+---+---+---+
  0   1   2   3   4   5

So if you wanted "Y", you could just type
"PYTHON"[1] (always start counting from 0!)
"""
fifth_letter = "MONTY"[4]

print fifth_letter

2.4 String methods

We'll focus on four string methods:

  1. len()
  2. lower()
  3. upper()
  4. str()

Let's start with len(), which gets the length (the number of characters) of a string!

parrot='Norwegian Blue'
print len(parrot)
The output will be the number of letters in  "Norwegian Blue" ,14

You can use the lower() method to get rid of all the capitalization in your strings.You call lower() like so:

"Ryan".lower()
which will return  "ryan" .

A similar method exists to make a string completely upper case.

parrot = "norwegian blue"

print parrot.upper()
Now let's look at  str() , which is a little less straightforward. The  str() method turns non-strings into strings! For example:

str(2)
would turn  2  into  "2" .

2.5 Dot Notation

Let's take a closer look at why you use len(string) and str(object), but dot notation (such as "String".upper()) for the rest.
Methods that use dot notation only work with strings.On the other hand, len() and str()can work on other data types.

ministry = "The Ministry of Silly Walks"

print len(ministry)
print ministry.upper()

2.6 Print String

print "Monty Python"
"""Assign the string "Ping!" to
the variable the_machine_goes on
line 5, then print it out on line 6!"""

the_machine_goes = "Ping!"
print the_machine_goes

2.7 String Concatenation

The + operator between strings will 'add' them together, one after the other.
print "Life " + "of " + "Brian"
This will print out the phrase  Life of Brian .
Sometimes you need to combine a string with something that isn't a string. In order to do that, you have to convert the non-string into a string.
# Turn 3.14 into a string on line 3!

print "The value of pi is around " +str(3.14)
This will print The value of pi is around 3.14.
The % operator after a string is used to combine a string with variables. The % operator will replace a %s in the string with the string variable that comes after it.
string_1 = "Camelot"
string_2 = "place"

print "Let's not go to %s. 'Tis a silly %s." % (string_1, string_2)
Let's not go to Camelot. 'Tis a silly place.
Remember, we used the % operator to replace the %s placeholders with the variables in parentheses.You need the same number of %sterms in a string as the number of variables in parentheses:
name = raw_input("What is your name?")
quest = raw_input("What is your quest?")
color = raw_input("What is your favorite color?")

print "Ah, so your name is %s, your quest is %s, " \
"and your favorite color is %s." % (name, quest, color)

2.9 Review

Three ways to create strings
'Alpha'
"Bravo"
str(3)
String methods
len("Charlie")
"Delta".upper()
"Echo".lower()
Printing a string
print "Foxtrot"
Advanced printing techniques
g = "Golf"
h = "Hotel"
print "%s, %s" % (g, h)








猜你喜欢

转载自blog.csdn.net/u011582611/article/details/72962403
今日推荐