done

More on Strings

From the beginning of this book we have used strings to represent words or phrases that we wanted to print out. A string is simply some characters inside quotes. In this chapter we explore strings in much more detail.

2.1 String indexing

We often need to access individual characters in a string. The characters are accessed by their position or index value. Python uses square brackets to enclose the index, [index number], to select a single character from a string.

Ex. 2.1-1 index_string.py

>>> s = 'Seattle Seahawks'
>>> s[0]
'S'
>>> s[1]
'e'
>>> s[7]
' '
...
>>> s[15]
's'
>>> s[-1]
's'
>>> s[-2]
'k'
  • The 16 characters are indexed left to right from postion 0 to position 15.
  • Negative indexing: the positions are named from right to left using negative numbers, where -1 is the rightmost index and so on.
  • Note that the character at index 7 (or -9) is the blank character.
  • If you try to index past the right end of the string, you will get 'out of range' error.

Strings are immutable Item assignment does not work for strings. Recall that strings are immutable.




Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: 'str' object does not support item assignment

Ex. 2.1-2 blueberry.py

>>> food ='blueberry muffen'
>>> m = food[3]
>>> print (m)
'e'
>>> n = food[4:9]
>>> print(n)
'berry'     # slice from index 4 to 9 (9 not included)

Slicing Strings

  • s[start:stop] can extract a substring from string s, starting at index start(number) and ending at index end-1.

  • s[start:stop:step] can give a slice of string s, starting at index start number to index stop number with step

  • step number is -1 means going backward

See examples: Ex. 2.1-3 str_slices.py

# str_slices.py
>>> numStr = '0123456789'
>>> print( numStr[3:7]
3456
>>> print( numStr[1:8:2]
1357
>>> print( numStr[6:2:-1]
6543
>>>
>>> s = 'RedBlueOrange'
>>> print( s[3:7])
Blue
>>> print( s[6:2:-1]
eulB

String Length To determine the length of a string, use len() function Ex. 2.1-3 string_len.py

>>> s= 'Apple pie'
>>> len(s)
9
>>> len( 'Banana float')
12
>>> len('')  # empty string
0
>>>len('donut')+len('pan cake')+10
23       # len() returns a integer
>>>

2.2 String Operation

'Addition' and 'Multiplication'

Add two strings(objects). 'a' + 'b' gives 'ab' (# Yes, values of the same type can be added together, not only just numbers.) But actually, the + operator represents concatenation, the two strings are linked together. Ex. 2.2-1 add.py

>>> a= "Hello,"  
>>> b= "BuzzCoders!"
>>> print (a+b)
Hello,BuzzCoders!
>>> print ( a+" "+b )
Hello, BuzzCoders!

'Multiply' a number and an string returns the string repeated that many times. 'la' 3 gives 'lalala' One of the operands has to be a string and the other has to be an integer. *Ex. 2.2-2 multiply.py

# Multiplication
>>> a = "Hello "
>>> print (a*5)
Hello Hello Hello Hello Hello
>>> print (a*0)
" "              # empty string
>>> s = '5'      # 5 is stored as a string
>>> print (s*5)
55555            # not 5*5=25,  s is a string '5'
>>> print (s * (-5))
""    
# string multiplied by a negative number gives an empty string

Ex.2.2-3 add_multiply.py

>>>s = "Coding rocks"
>>>print(s[:2] + s[-4]+s[-2:])
Cooks
>>>
>>>print('La '*2+'Land')s
La La Land
>>>
>>>name = 'bear'
>>>print('Three little '+name+ 's jumping on the bed
          \nshouting '+name1*3)

Three little bears jumping on the bed
shouting bearbearbear

Ex. 2.2-4 reverse_str.py Reverse 'Hello' to be 'olleH'. (If you want reverse a number 543 to be 345, you can treat the number to be a string. Later you will see another very easy way to use a loop on a long string)

# reverse_str.py
>>> s = 'Hello'
print (s[-1]+s[-2]+s[-3]+s[-4]+s[-5])

Comparing Strings Ex. 2.2-5 compare.py

>>> a = 'Hello'
>>> b = 'Hello '
>>> a==b       # b has a space at the end
False
>>> a,b = ('Hello', 'hello')
>>> a==b       # case sensitive
False

Paliindrome A fun program to look at: We take that text and reverse it. If the original text and reversed text are equal, then the text is a palindrome.(?find some words online)

Ex. 7.6-13 paliindrome.py

def reverse(text):
    return text[::-1]


def is_palindrome(text):
    return text == reverse(text)


something = input("Enter text: ")
if is_palindrome(something):
    print("Yes, it is a palindrome")
else:
    print("No, it is not a palindrome")

2.3 String Methods:

There are many useful string methods(function) we can use to manipulate a string.

The “dot notation” is the way we connect the name of a string (object) to the name of a method it can perform.

string.upper() string.lower()
string.capitalize()
string.title() These methods don't change what's stored in the variable.

Ex. 2.3-1 string_methods.py

>>> title = "the book thief"
>>> t_up = title.upper()
>>>print (t_up)
THE BOOK THIEF 
>>> title.lower()
the book thier
>>> title.capitalize()
The book thief
>>> title.title()
The Book Thief
>>> print title
the book thief   # the original value unchanged

Ex. 2.3-2 pig_latin.py Move the first letter of a word to the end and add 'ay'. 'Banana' in Pig Latin it's 'Ananabay'

>>># pig_latin.py
>>> word = input('Enter a word: ')
>>> w1 = word[0]
>>> print('Translate into Pig Latin is: ')
>>> print(word[1:].capitalize()+ w1.lower()+ 'ay')
Enter a word: Hello
Translate into Pig Latin is:
Ellohay

===

(Pro topic)

More String Methods

string.strip() removes all whitespace around a string

Ex. 2.3-3 strip.py

>>> name = '  Aloha  '
>>> name.strip()
Aloha
>>> name='***Sniffy****'
>>>name.strip('*')   # remove all * around
Sniffy
>>> name.rstrip('*') # remove all the trailing * 
***Sniffy
>>> name.lstrip('*')  # remove all the leading * 
Sniffy****

Searching and Replacing Text string.count(str) string.find(str) index(str) replace(old str, new str)

Ex. 2.4-4 more_sMethods.py

>>>s='red berry blue berry purple apple'
>>>s.count('berry')
2    # counts how many times 'berry' occurs in s        
>>>s.find('berry')
4    # return the index of the first 'berry'
>>> 
>>>print(s.replace('apple','car')
red berry blue berry purple car
>>>print(s.replace('apple','fish')
          .replace('berry','crab'))
red crab blue crab purple fish
# replace() can be used one after another

===

2.4 Formatting Strings with format % value

There are some ways in Python to format strings, it's goal is to present string in a form that's easy to understand and good looking.

Inserting string values into a string, %s Python supports formatting values into strings. Although this can include very complicated expressions, the most basic usage is to insert values into a string with the %s placeholder.

If you want to display a message using the contents of a variable, you can use %s to embed values in the string, %s is like a place holder for a value you want to add later.

Ex. 2.4-1 insert_valueS.py

>>># insert a string value into a message string
>>>myName = 'Buzz'
>>>print ('My name is %s.' % (myName) )
>>>
My name is Buzz.

Here we have a variable myName to hold a string value 'Buzz', when I print the message string 'My name is %s.', %s is a placeholder for the 'myName' variable value, after the message string, followed a % symbol and (myName) to tell Python to replace %s in the message string with the value stored in variable 'myName'.

We can pass in different values for the %s placeholder, using different variables, see examples:

Ex. 2.4-2 insert_valueS2.py

>>> # insert two strings into a message string
>>>myName = 'Buzz'
>>>book = 'The Book Thief'
>>>print("My name is %s, I like to read '%s'."
          % (myName, book) )
>>>
My name is Buzz, I like to read 'The book Thief'

The whole expression evaluates to a string. The first %s is replaced by the value of myName; the second %s is replaced by the value of book. All other characters in the string stay as they are.

Insert integers with %d String formatting works with integers by specifying %d instead of %s. The values provided must match exactly as placeholders required. (both in number, order and type)

Ex. 2.4-3 insert_valueN.py

# insert an integer value into a message string
>>>myName = 'Yihaa'
>>>age = 10
>>>print ("My name is %s, I'm %d years old." 
           % (myName, age) )
      # %s is for strings, %d is for digits
>>>
My name is Yihaa, I'm 10 years old.

Ex. 2.4-4 chair_joke.py

>>>joke ='%s is a device for annoying people 
          in the dark.'
>>>furniture = 'Chair'
>>>num = 16
>>>print (joke % furniture )
>>>print ('I have %d of them in my small 
           living room! ')
>>>
Chair is a device for annoying people in the dark.
I have 16 of them in my small living room!

==== ???

Formatting different type values

%s instead of %d You may have noticed that %s is accepted to use instead of %d for numbers, while %d is not permitted to replace %s.

Ex. 2.4-5 digit_placeholder.py

>>>name = 'Heya'
>>>age = 12
>>>print('I\'m %s, I\'m %d.' % (name, age))
I'm Heya, I'm 12.
>>>print('I\'m %s, my age is %s.' % (name, age))
I'm Heya, my age is 12.
>>> #Notice, %s is Ok to be placeholder for numbers.

This is because:

  • When using the %s formatter, any object is implicitly converted to a string by using the str().
  • These rules are not defined for, for example, an integer: the %d formatter does not imply a call to int(), since this is not always defined (for example, int("Meow") will raise a ValueError).

Format different types of values String formatting in Python has many options, add modifier strings to format many different types of values.

%f, %.2f, %+.2f formatting floating point numbers

Ex. 2.4-6 float.py

>>>x= 1/7   
>>>print (x)
0.14285714285714285
>>>print('value: %f' % x)
0.142857
>>>
>>>print ('value: %.2f' % x)
value: 0.14
>>>print ('value: %.5f'% x)
value: 0.14286
>>>
  • The %f string formatting option treats the value as a decimal, and prints it to six decimal places.
  • The ".2" modifier of the %.2f option round the value to two decimal places.

Ex. 2.4-7 mix_type.py

>>>a,b,c= 6, 3.1415, 'cat'
>>>s='There are %d %ss older than %.2f years.'
      % (a,c,b) 
>>>print (s)
There are 6 cats older than 3.14 years.

Some other conversion specifiers specifier meaning

  • d integer
  • s string
  • f float
  • e lower case float exponetial
  • % character

2.5 Format() function

%s is easy to use when inserting values into strings. A another way to create fancy strings is to use string function format().

str.format() syntax may be as simple as two curly brackets {} that specify a placeholder for data. You can number the placeholder like {0} would contain the first data, and {1} is for the second data.

Ex. 2.5-1 I_like.py

>>>print('I like {} very much!'.format('ice cream'))
I like ice cream very much!
>>>print('{0} is a {1}'.format('Sniffy','hamster'))
Sniffy is a hamster.
>>>

More example: Ex. 2.5-2 sniffy_facts.py

>>>name = 'Sniffy'
>>>height = 3
>>>color_1 = 'Blue'
>>>color_2 = 'Brown'
>>>
>>>print ('Let\'s talk about {0}, he is {1} inches tall.'.format(name, height) )
>>>
Let's talk about Sniffy, he is 3 inches tall.
>>>print ('He\'s got {0} eyes and {1} hair, 
   he likes to eat {0} cheese.'.format(color_1, color_2) )
>>>
He's got Blue eyes and Brown hair, he likes to eat Blue cheese.
>>>  # Notice we use '{0}' twice, so the value 'Blue' repeats in the sentence.

**When you put numbers in the placeholders, you can repeat them, the corresponding data elements will appear more than once.

Accessing arguments by position: Ex. 2.5-3 position.py

>>>
>>> '{0}, {1}, {2}'.format('a', 'b', 'c')
'a, b, c'
>>> '{}, {}, {}'.format('a', 'b', 'c')  
'a, b, c'
>>> '{2}, {1}, {0}'.format('a', 'b', 'c')
'c, b, a'

Ex. 2.5-4 repeat, unpack

**Arguments' indices can be repeated

# repeat.py
>>> '{0}{1}{0}'.format('ab1  ', 'de2  ')  
'ab1  de2  ab1'

**Unpacking argument sequence

# unpack.py
>>> '{2}, {1}, {0}'.format(*'abc')                 
'c, b, a'

Accessing arguments by name: keyword-based You can also use variable names(keyword) to point to the corresponding value, see example:

Ex.2.5-5 name_access.py

>>>print('The {color} {pet} broke the flower pot.'
          .format(color = 'yellow', pet='dog'))
The yellow dog broke the flower pot.

Do formatting with { } All the previous examples we only use {} as placeholders for values, we can also do special formatting with a colon inside the {} and followed with the formatting we like to use.

str.format() compare with the old % formatting

In most of the cases the syntax is similar to the old % formatting. Instead of %, we use the {} and with : followed by formatting specifier. For example, '%.2f' can be translated to '{:.2f}'.

Ex 2.5-6 float_format.py

>>>print('Pi is %.2f' % (3.1415926))
Pi is 3.14
>>>print('Pi is {:.2f}'.format(3.1415926))
Pi is 3.14
# same results in both cases

2.6 Pro topic

More on 'format % values'

String objects have one unique built-in operation: the % operator (modulo). This is also known as the string formatting or interpolation operator. Given format % values (where format is a string), % conversion specifications in format are replaced with zero or more elements of values.

If format requires a single argument, values may be a single non-tuple object. Otherwise, values must be a tuple with exactly the number of items specified by the format string, or a single mapping object (for example, a dictionary). In simple words, values must match placeholders.

A conversion specifier contains two or more characters and has the following components, which must occur in this order:

  • 1.The '%' character, which marks the start of the specifier.

  • 2.Mapping key (optional), consisting of a parenthesised sequence of characters (for example, (somename)).

  • 3.Conversion flags (optional), which affect the result of some conversion types.

  • 4.Minimum field width (optional). If specified as an '*' (asterisk), the actual width is read from the next element of the tuple in values, and the object to convert comes after the minimum field width and optional precision.

  • 5.Precision (optional), given as a '.' (dot) followed by the precision. If specified as '*' (an asterisk), the actual precision is read from the next element of the tuple in values, and the value to convert comes after the precision.

  • 6.Length modifier (optional).

  • 7.Conversion type.

When the right argument is a dictionary (or other mapping type), then the formats in the string must include a parenthesised mapping key into that dictionary inserted immediately after the '%' character. The mapping key selects the value to be formatted from the mapping. For example:

Ex. 2.6-1 wizard_in_college.py

>>>
>>> print('%(college)s college has 
           %(number)05d   wizard types.' %
      {'college': "Hogwarts", "number": 30})
Hogwarts college has 00030 wizard types.

In the above example, the conversion specifiers have components 1.% operator; 2.Mapping key in parenthesis, (college) and (number); 6. Length modifier, the 05 in '%05d'; 7. Conversion type, s is for string, d is for integer. The right argument is a dictionary.

More on the str.format() method

As we menssioned before,We can create a formatting specification as part of a string and then use format() function to add data to that string.

Format strings contain “replacement fields” surrounded by curly braces { }. Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling: .

What Python does in the format method is that it substitutes each argument value into the place of the specification. There can be more detailed specifications such as:

Ex. 2.6-2 more_format.py

>>>print('{:.2f}'.format(1.0/3) )
0.33
# decimal precision, 2 digits after decimal point
>>>
>>>print('{:010.2f}'.format(3.14159265) )
0000003.14       
# 10 digits wide, 
# (0 after :) filled with 0, 
# (.2 before f) 2 digits after decimal point, 
# (f) float 
>>>
>>>print('{:*^10.2f}'.format(3.14159265) )
***3.14***
# 10 digits wide, 
# (^) centered and filled with *, 
# (.2) 2 digits after decimal point float
>>>
>>> print('{:,}'.format(1234567890) )
1,234,567,890
# Using the comma as a thousands separator:
>>>
>>> points = 19
>>> total = 22
>>> print('Correct answers:{:.2%}'.format(points/total))
Correct answers: 86.36%
# % indicate percentage

Here is a summery of some commonly used specifications:

  • fill: character used to fill if the data is too small to fit the assigned space
  • align: < left aligned; > right aligned; ^ centered
  • sign: + positive numbers: - negative numbers

    ' ' space: a space in front of positive number and minus sign before negative number

  • width: assign space to data
  • , : use commas as a thousands separator for numeric data
  • .precision: how many digits after decimal point
  • type: the output type

    1 String, s or nothing 2 Integer, d for decimal 3 Floating point, --- f lowercase fixed point; --- % percentage
    --- 'e/E' exponent using e/E as a separator

results matching ""

    No results matching ""