Python .replace 'st' with 'street' but leave street name the same

I'm trying to change st. to street, ave. to avenue, etc. using .replace() for addresses in a single cell. For example: WEST ST. should be WEST STREET or MOUNT PEBBLE RD. should be MOUNT PEBBLE ROAD

Here is my code:

if 'STREET' not in address and address.find('ST.'):
    address = address.replace('ST','STREET')

The result gives me WESTREET STREET. How can I leave the address name untouched without altering the address name? I tried .split() but most cells had different list lengths so it got really confusing.

Try this:

if 'STREET' not in address and address.find('ST.'):
    address = address.replace(' ST.',' STREET')

Python String, replace() is an inbuilt function in Python programming language that returns a copy of the string where all occurrences of a substring is replaced with another  Python String replace () Method Definition and Usage. The replace () method replaces a specified phrase with another specified phrase. Note: All Syntax. Parameter Values. A number specifying how many occurrences of the old value you want to replace. More Examples.

Try this, the less the code the better.

address = "WEST ST."

if address.find('ST.'):
    address = address.replace(' ST.',' STREET')

print(address)

Python String replace() Method, Python String replace() Method - Python string method replace() returns a copy of the string in which the occurrences of old have been replaced with new,  Python String replace () replace () parameters. Note: If count is not specified, the replace () method replaces all occurrences of the old Return Value from replace (). The replace () method returns a copy of the string where the old substring is replaced Example 1: Using replace (). More

Try using Regex with boundaries.

Ex:

import re

s = """WEST ST
       MOUNT PEBBLE RD"""

toReplace = {"ST": 'STREET', "RD": "ROAD", "ave": "avenue"}
for k,v in toReplace.items():
    s = re.sub(r"\b" + k + r"\b", v, s)
print(s)

Output:

WEST STREET
MOUNT PEBBLE ROAD

Python String replace(), The replace() method returns a copy of the string where all occurrences of a substring is replaced with another substring. The replace() method is part of the string module, and can be called either from a str object or from the string module alone. Python's string.replace() Prototype. The prototype of the string.replace() method is as follows: string.replace(s, old, new[, maxreplace]) Function parameters s: The string to search and replace from.

I edited your code like this:

address = "WEST ST."

if 'STREET' not in address and address.find('ST.'):
    address = address.replace('ST.','STREET')

print (address)

Here is the result:

WEST STREET

You have to replace "ST." not "ST"

7.1. string — Common string operations, vformat() does the work of breaking up the format string into character data and replacement fields. It calls the various methods described below. In addition, the​  Python 3 - String replace () Method Description. The replace () method returns a copy of the string in which the occurrences of old have been replaced with Syntax. Parameters. Return Value. This method returns a copy of the string with all occurrences of substring old replaced by new. If the

Replace strings in Python (replace, translate, re.sub, re.subn), Here's how to replace strings in Python.Replace substrings: replace()Specify the maximum count of replacements: countReplace multiple  When value=None and to_replace is a scalar, list or tuple, replace uses the method parameter (default ‘pad’) to do the replacement. So this is why the ‘a’ values are being replaced by 10 in rows 1 and 2 and ‘b’ in row 4 in this case. The command s.replace('a', None) is actually equivalent to s.replace(to_replace='a', value=None

How to use string.replace() in python 3.x, As in 2.x, use str.replace() . Example: >>> 'Hello world'.replace('world', 'Guido') '​Hello Guido'. In Python string literals, backslash is an escape character. This is also true when the interactive prompt shows you the value of a string. It will give you the literal code representation of the string.

replace, Returns a copy of the string with a specified substring replaced specified number of times. Syntax¶. str. replace(old, new[, count]). old: Required. String to be  In Python 2 I create digest value with hashlib.md5 from given string and apply some logic on the output string e.g. ord function on string characters. I migrate the code to Python3 and face two problems: 1st: md5 expectst bytes and outputs bytes -> so I give it latin1 encoded string as per default Python2 encoding.

Comments
  • Can st. appear at the begin of the string?
  • Is there any . in the string that is not part of an abbrev.? For example "West."?
  • Add single space before ST and STREET in replace address = address.replace(' ST',' STREET')
  • The reason why your code wasn't running was because there needs to be a period after ST in your .replace() code
  • I have added . with ST. You can use both as per your need.
  • @muffint0p you can accept the answer if it helps :).
  • You could also do this address = address.replace(' ST',' STREET')