Menu Close

String replace, concatenate, split, reverse, uppercase, and lowercase

In python, there is often a need to modify a string in different ways. Instead of reassigning these values, we can use pre-existing methods for these strings.

Replace

The replace method swaps values or terms in a string with another value. The syntax is as follows:

string.replace(toReplace, replaceWith, count)

*if count is blank, it will replace all terms.

Example 1:

The code below replaces every E in a string with the letter A.

myString = “Every python programmer should use comments.”
newString = myString.replace(“e”, “a”)
print(newString)

Output:

Evary python programmar should usa commants.

In the code above, the capital E is not replaced as capital and lowercase letters are seen as different character types in Unicode.

Example 2:

The code below replaces the first 4 of every address in the list with a 5.

addressList = [“2884 Small Street, New York”, “1844 Goldcliff Circle, Washington DC”, “4044 West Drive, Chicago”]

for address in addressList:
    print(address.replace(“4”, “5”, 1))

Output:

2885 Small Street, New York
1854 Goldcliff Circle, Washington DC
5044 West Drive, Chicago

Concatenate

Concatenation is the process of combining multiple strings together. Rather than using a special concatenation method, we use an operator instead “+”.

Example 1:

stringOne = “My first string”
stringTwo = “My second string”

concatString = stringOne + stringTwo
print(concatString)

Output:

My first stringMy second string

Alternatively, if strings can be multiplied using “*” to repeat the string many times

Example 2:

testString = “Python”
finalString = testString * 5

print(finalString)

Output:

PythonPythonPythonPythonPython

Split

Often in python, programmers want to split one string into multiple different strings. This can be achieved through the split method. By determining a value to split by, it can split a string by that value, and put all values into a list. Below is the syntax of the split method:

string.split(splitValue, count)

*if splitValue is blank, it will split by whitespaces by default. If count is blank, it will split by every occurrence of the splitValue.

Example 1:

myString = “The quick brown fox jumps over the lazy dog”
print(myString.split())

Output:

[‘The’, ‘quick’, ‘brown’, ‘fox’, ‘jumps’, ‘over’, ‘the’, ‘lazy’, ‘dog’]

Example 2:

myString = “John, Bobby, Arnold”
firstValue, otherValues = myString.split(“, “, 1)
print(firstValue)
print(otherValues)

Output:

John
Bobby, Arnold

As shown in the example above, you can use separate variables rather than have every value inserted into a new list.

Reverse

Unfortunately, there is no built-in reverse method for python strings. However, using clever indexing, we are still able to reverse the order of characters in a string.
The syntax for such is as follows:

String[::-1]

The reason why this works is that leaving the first 2 values blank represents every value in the String.
The -1 at the end will go through every character starting from the back to the front.

Example:

myString = “Python is fun!”
newString = myString[::-1]
print(newString)

Output:

!nuf si nohtyP

Uppercase

It can sometimes be helpful to make characters in a string all uppercase. This can be achieved through the upper() method. The syntax is as follows:

string.upper()

*symbols and numbers are ignored

Example:

myString = “123 Vancouver Plaza”
newString = myString.upper()
print(newString)

Output:

123 VANCOUVER PLAZA

Lowercase

It can be very helpful to make all letters in a string lowercase. This can help with case insensitivity in a program. This is achieved through the lower() method. The syntax is as follows:

string.lower()

*again, symbols and numbers are ignored

Example:

myString = “This sentence has MANY CAPITALS”
newString = myString.lower()
print(newString)

Output:

this sentence has many capitals

 

Posted in Embedded Programming Language, Python

Related Articles

Leave a Reply

Your email address will not be published.

Leave the field below empty!