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.
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.
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)
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.
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))
2885 Small Street, New York 1854 Goldcliff Circle, Washington DC 5044 West Drive, Chicago
Concatenation is the process of combining multiple strings together. Rather than using a special concatenation method, we use an operator instead “+”.
stringOne = “My first string” stringTwo = “My second string” concatString = stringOne + stringTwo print(concatString)
My first stringMy second string
Alternatively, if strings can be multiplied using “*” to repeat the string many times
testString = “Python” finalString = testString * 5 print(finalString)
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:
*if splitValue is blank, it will split by whitespaces by default. If count is blank, it will split by every occurrence of the splitValue.
myString = “The quick brown fox jumps over the lazy dog” print(myString.split())
[‘The’, ‘quick’, ‘brown’, ‘fox’, ‘jumps’, ‘over’, ‘the’, ‘lazy’, ‘dog’]
myString = “John, Bobby, Arnold” firstValue, otherValues = myString.split(“, “, 1) print(firstValue) print(otherValues)
John Bobby, Arnold
As shown in the example above, you can use separate variables rather than have every value inserted into a new list.
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:
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.
myString = “Python is fun!” newString = myString[::-1] print(newString)
!nuf si nohtyP
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:
*symbols and numbers are ignored
myString = “123 Vancouver Plaza” newString = myString.upper() print(newString)
123 VANCOUVER PLAZA
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:
*again, symbols and numbers are ignored
myString = “This sentence has MANY CAPITALS” newString = myString.lower() print(newString)
this sentence has many capitals