Back

Python Chrestomathics

Ahead


 

Part 5 — String massage

Manipulating — Not something I like to do — But with strings it’s fine

swirl © noun project swirl © noun project swirl © noun project swirl © noun project


Section 5.1: Stripping

Stripping to basics — Makes things easier to see — And patterns emerge

erase © noun project erase © noun project


# Get text

reply = input( "Enter text: " )

# Get a cleaned version of the text

s = reply.strip()

# Show the differences in the strings

print( "string of interest:", s, ":" )

print( " stripped version:", t, ":" )

Enter text: scraunched is just one syllable

Variable Value
reply "    scraunched is just one syllable  "

s = reply.strip()

Variable Value
reply "    scraunched is just one syllable  "
s "scraunched is just one syllable"

string of interest: scraunched is just one syllable :

  stripped version: scraunched is just one syllable :



Section 5.2: Proper casing

Who, WhaT, wHy, and whEN — WEIRD CaPiTaLiZaTion — Can be too glaring

sun © noun project

cover of Dean Martin Single Thats Amore copyright Capitol Records

© Capital Records

# Set string of interest

s = "When an Eel Climbs a Ramp to Eat Squid From a Clamp, That’s a Moray"

# Get different capitalization versions

v1 = s.lower() # Get copy with letters in lower case

v2 = s.upper() # Get copy with letters in upper case

v3 = s.capitalize() # Get copy with first character upper case, rest

  # lower case

# Print results

print( "s:", s )

print()

print( "s.lower():", v1 )

print( "s.upper():", v2 )

print( "s.capitalize():", v3 )

s: When an Eel Climbs a Ramp to Eat Squid From a Clamp, That’s a Moray

s.lower(): when an eel climbs a ramp to eat squid from a clamp, that’s a moray

s.upper(): WHEN AN EEL CLIMBS A RAMP TO EAT SQUID FROM A CLAMP, THAT’S A MORAY

s.capitalize(): When an eel climbs a ramp to eat squid from a clamp, that’s a moray



Section 5.3: Finding

Finding what you need — Takes an instant or lifetime — Me: love at first sight

find © noun project

s = "can anyone"

t = "an"

i1 = s.find( t ) # Finds first occurrence of string t within s

i2 = s.find( t, i1 +1 ) # Finds first occurrence of string t within s

  # starting at index i1 + 1

# Initialize strings

s = "can anyone"

t = "an"

# Find occurrences

i1 = s.find( t )

i2 = s.find( t, i1 + 1 ) # Start looking for next occurrence right after

i3 = s.find( t, i2 + 1 ) # where the previous occurrence started

# Print results

print( "s:", s )

print( " ==========" )

print( " 0123456789 <== indices into s" )

print( "t:", t )

print()

print( "s.find( t ):", i1 )

print( "s.find( t,", i1, "):", i2 )

print( "s.find( t,", i2, "):", i3 )

s: can anyone

  ----------

  0123456789 <== indices into s

t: an

s.find( t ): 1

s.find( t, 1 ): 4

s.find( t, 4 ): -1

Variable Value
s "can anyone"
t "an"

i1 = s.find( t )

Variable Value
s "can anyone"
t "an"
i1 1

i2 = s.find( t, i1 + 1 )

Variable Value
s "can anyone"
t "an"
i1 1
i2 4

i3 = s.find( t, i2 + 1 )

Variable Value
s "can anyone"
t "an"
i1 1
i2 4
i3 -1



Section 5.4: Accessing

Need to get inside — Is easy with the right key — For strings it’s brackets

key access © noun project

c = reply[ i ]

# Get three indices of user interest

reply = input( "Enter three indices: " )

i1, i2, i3 = reply.split()

i1 = int ( i1 )

i2 = int ( i2 )

i3 = int ( i3 )

Enter three indices: 0 8 9

Variable Value
reply "0 8 9"
i1 0
i2 8
i3 9

# Initialize target string

s = "chrestomathics"

# Use the indices to pick off single characters from s

c1 = s[ i1 ] # Grab character at index i1 in s

c2 = s[ i2 ] # Grab character at index i2 in s

c3 = s[ i3 ] # Grab character at index i3 in s

Variable Value
reply "0 8 9"
i1 0
i2 8
i3 9
s "chrestomathics"
c1 "c"
c2 "a"
c3 "t"

# Use the characters to make a new string

t = c1 + c2 + c3

Variable Value
reply "0 8 9"
i1 0
i2 8
i3 9
s "chrestomathics"
c1 "c"
c2 "a"
c3 "t"
t "cat"

# Print result

print( t )

cat

Enter three indices: 13 1 3

she

Enter three indices: 2 8 7

ram



Section 5.5: Slicing

A slice of a pie — Fresh just out of the oven — Is a dream come true

pie © noun project

# Get string of choice

s = input( "Enter favorite pie: " )

# Get indices i and j

t = input( "Enter two indices i and j: " )

i, j = t.split()

i = int( i )

j = int( j )

Enter favorite pie: chocolate

Enter two indices i and j: 3 7

Variable Value
s "chocolate"
i 3
j 7

# Get slices of s

slice1 = s[ i : j ] # Substring starts at index i, ends at index j-1

slice2 = s[ i : ] # Substring starts at index i, continues to end of string

slice3 = s[ : j ] # Substring starts at index 0, ends at index j-1

slice4 = s[ : ] # Substring equals the entire string

Variable Value
s "chocolate"
i 3
j 7
slice1 "cola"
slice2 "colate"
slice3 "chocola"
slice4 "chocolate"

Enter favorite pie: chocolate

Enter two indices i and j: 3 8

s[ i : j ]: colat

s[ i : ]: colate

s[ : j ]: chocolat

s[ : ]: chocolate

Enter favorite pie: pizza

Enter two indices i and j: 2 3

s[ i : j ]: z

s[ i : ]: zza

s[ : j ]: piz

s[ : ]: pizza

Enter favorite pie: garlic tart

Enter two indices i and j: 1 8

s[ i : j ]: arlic t

s[ i : ]: arlic tart

s[ : j ]: garlic t

s[ : ]: garlic tart



Section 5.6: Replacement

result = text.replace( s, r )

# Get text, search string, and replacement string

text = input( "Enter text: " )

s = input( "Enter substring (s): " )

r = input( "Enter substring (r): " )

# Get version of text with occurrences of s replaced with r

result = text.replace( s, r )

# Print substitution

print()

print( "text.replace( s, r ):", result, " # text's s's replaced with r's" )

Enter text: hello mellow pillow

Enter substring (s): ll

Enter substring (r): L L

text.replace( s, r ): heL Lo meL Low piL Low # text’s s’s replaced with r’s

Enter text: jeepers creepers look at those peepers

Enter substring (s): eepers

Enter substring (r):

text.replace( s, r ): j cr look at those p # text’s s’s replaced with r’s



Section 5.7 String casting

formula = "H" + 2 + "O" # illegal use of + operator

formula = "H" + str( 2 ) + "O"



Section 5.8 What’s next


Back

Python Chrestomathics

Ahead

 


© Jim Cohoon. For now, all rights reserved.