Docsity

Working with Strings: String Concatenation, Formatting, and Methods - Prof. Lotfi Ben Othm, Study notes of Web Design and Development

Partial preview of the text

Download Working with Strings: String Concatenation, Formatting, and Methods - Prof. Lotfi Ben Othm and more Study notes Web Design and Development in PDF only on Docsity!

1/22/2009 1

Chapter 3:

Working with Strings

Lotfi Ben Othmane Department of Computer Science Western Michigan University

1/22/2009 2

Roadmap

 Overview

 String concatenation

 Raw string

 String formatting

 String Methods

1/22/2009 3

1. Overview

 A string is a value where its delimiters are single

quotes or double quotes

Print a value as it is in Python

String value

Print a string value

1/22/2009 4

1. Overviewโ€”Cont.

 A string is a mutable sequence

Can not change the sequence using slicing

1/22/2009 7

1.2. Raw String

 Use backslash โ€œ\โ€ to escape characters โ€œโ€™โ€ or โ€œ\โ€ in a

string value

 Prefix raw string with letter โ€œrโ€ to force Python to

print the string value as it is

Character โ€œโ€™โ€ couldnโ€™t be part of the string

Backslash character is used to display character โ€œโ€™โ€ in a string

Character โ€œ\nโ€ is considered line return Character โ€œ\nโ€ is escaped using โ€œ\โ€

Character โ€œrโ€ is used to require the print of the string as it is

1/22/2009 8

1.3 String Constants

Contant Constant Description

string.uppercase A string containing all uppercase letters

A string containing all punctuation

characters

string.punctuation

string.printable A string containing all printable characters

string.lowercase A string containing all lowercase letters

A string containing all letters (upper- and

lowercase)

string.letters

string.digits A string containing the digits 0โ€“

1/22/2009 9

2. String Formatting

 String formatting converts a variable to a string using specified

rules

 A conversion is specified using the following items:

 Character %: specifies the beginning of the conversion  Conversion flags: optional flags that could be used in the conversion Character โ€œ-โ€ : left alignment Character โ€œ+โ€ : A sign should proceed the converted values Character โ€œโ€ : A space should proceed positive numbers Character 0 : should be zero padded  Minimum field width  A. (dot) followed by the precision  Conversion type (see course book page 57 for the list of conversion types)

1/22/2009 10

2. String Formattingโ€”Cont.

Simple formatting

Formatting float using precision

Formatting string using precision Formatting float using padding

Formatting float using left alignment

1/22/2009 13

3. String Methods

 List of string methods

 find method

 join method

 lower method

 replace method

 split method

 strip method

 translate method

 Extensive list of string methods is available in

Appendix B-6 pp.586-

1/22/2009 14

3.1 find Method

 Find a substring within a string. If the substring is

found than return the leftmost index where the

substring is found. If the substring is not found return -

Return the position of the substring โ€œcs2100โ€

Return -1 because โ€œCs2100โ€ does not exist in the string Return the first position of the substring โ€œiโ€

Return the first position of the substring โ€œiโ€ such that the start position is 8 and the end position is the length of the string

1/22/2009 15

3.2 join Method

 Join the elements of a sequence

Create a tuple

Join a tuple elements

Create a list

Create a list elements

Error when joining integers using a character

1/22/2009 16

3.3 lower Method

 Return the lower case of the string

Convert the string to lower case

Find method returns -1! Why?

Lower method is used in a membership test statement

1/22/2009 19

3.6 strip Method

 Delete whitespaces from the left and the right of a

string

Delete while spaces on the left and right of the string

The first membership test returned negative result and the second test returned positive results! Why?

1/22/2009 20

3.6 translate Method

 Translate character by character a string

Import method maketrans from string library and construct a table string such that characters โ€˜cโ€™ and โ€˜sโ€™ are replaced respectively by โ€˜kโ€™ and โ€˜zโ€™

Translate a string using the table constructed above (make appropriate replacement of charactersโ€™

1/22/2009 21

Conclusion

 The chapter covered

 Use of string values

 Formatting of strings

 String Methods