Chapter 5



#Text is represented in programs by the string data type.
#Strings can also be saved in variables, just like any other data.
-------------------------
>>> str1 = "Hello"
>>> str2 = 'spam'
>>> print (str1, str2)
Hello spam
>>> type(str1)
class 'st'
>>> type(str2) class 'str'


#Recall that the input function returns whatever the user types as a string object.
-------------------------
>>> firstName = input("Please enter your name: ")
Please enter your name : John
>>> print ("Hello", firstName)
Hello John


#The rest of this section takes you on a tour of the more important Python string operations.
-------------------------
#Access the individual characters that make up the string.
#In Python, this can be done through the operation of indexing.


#Here are some interactive indexing examples:

>>> greet = "Hello Bob"
>>> greet[0]
'H'
>>> print (greet[O] , greet[2] , greet[4] )
H 1 o
>>> x = 8
>>> print (greet[x-2])
B

#Python also allows indexing from the right end of a string using negative indexes.

>>> greet[-1]
'b'
>>> greet [-3]
'B'


#A slice produces the substring starting at the position given by start and running up to, but not including, position end.#
-------------------------
>>> greet [0: 3]
'Hel'
>>> greet [5: 9]
'Bob'
>>> greet[:5]
'Hello'
>>> greet[5:]
'Bob'
>>> greet[:]
'Hello Bob'

#The last three examples show that if either expression is missing,
#the start and end of the string are the assumed defaults.
#The final expression actually hands back the entire string.



#Concatenation builds a string by "gluing" two strings together.i>
-------------------------
>>> "spam" + "eggs"
'spameggs'
>>> "Spam" + "And" + "Eggs"
'SpamAndEggs'
>>> "Spam" + " " + "and" + " " + "Eggs"
'Spam and Eggs'
>>> len("spam")
4
>>> "spam" * 5
'spamspamspamspamspam'


#Python string operations





-------------------------
#Strings are always sequences of characters, whereas lists can be sequences of arbitrary objects.
>>> myList = [1, "Spam", 4, "U"]
>>> myList [0]
1

#SEE month.py and month2.py programs