ABOUT PYTHON - STRING MANIPULATION
If you already know about the string data type. In this story, we will discuss how to process or manipulate string data.
A string in Python can be written in single or double quotes.
The advantage of using double quotes is that we can insert single quotes in that string.
Example:
name = "David 'Adam"
print(name)Output:
David 'AdamWhen we use a single quotation mark to enclose a string and we want to insert a quotation mark single quotes are also in the string, then how to add a backslash character in front of the quotation marks.
Example:
name = 'David \'Adam'
print(name)Output:
David 'AdamEscape Characters
Escape characters are characters that have special meaning. Table 9.1 shows some of them
Example:
myString = "Hello\nWorld"
print(myString)Output:
Hello
WorldWhen the example is observed, then the addition of the character \n in front of ‘World’ will affect the line replacing the string.
Also, consider the following example for using the \t character which has the same meaning as given tabs.
myString = "Hello\tWorld"
print(myString)Output:
Hello WorldOperators “in” and “not in” in String
Like lists, tuples, and dictionaries, you can also use the in and not in operators for strings. This operator is used to check whether a substring is contained in the string.
Example:
myString = "Hello World, I'm Fine"
print('World' in myString)
print('world' not in myString)
print('hello' in myString)Output:
True
True
FalseFrom the example above, command ‘World’ in myString to check if the string ‘World’ exists inside the myString or not. In this case yes, so it produced True. Whereas for ‘hello’ in myString will return False value because the string ‘hello’ is different from ‘Hello’ in myString.
Upper () and Lower () Commands
The upper() command is used to convert a string to all capital letters. Whereas lower() to convert the string to lowercase.
Example:
myString = "Hello World, I'm Fine"
print(myString.upper())
print(myString.lower())Output:
HELLO WORLD, I'M FINE
hello world, i'm fineJoin() Command
In addition to using the string concatenation operator (+), we can also perform string concatenation with the join() command. The advantage of join() is that we can specify characters that are a link between the concatenated strings.
Get Danang Kurniawan’s stories in your inbox
Join Medium for free to get updates from this writer.
Example:
combine = ', '.join(['mango', 'apple', 'melon'])
print(combine)combine = ' and'.join(['mango', 'apple', 'melon'])
print(combine)combine = ' '.join(['mango', 'apple', 'melon'])
print(combine)
Output:
mango, apple, melon
mango and apple and melon
mango apple melonThe combine command = ', '.join(['mango', 'apple', 'melon']) will
concatenates all the strings 'mango', 'apple', and 'melon'. Then between the strings, there is a string ‘, ’ so the string ‘mango, apple, melon’ will be produced. The same applies to the combine commands = ‘ and ‘.join([‘mango’, ‘apple’, ‘melon’]) and combine = ‘ ‘.join([‘mango’, ‘apple’, ‘melon’]).
Replace() Command
The replace() command is used to convert a substring in one string into another.
Example:
myString = "I LIKE PYTHON"
myString.replace("LIKE", "LOVE")
print(myString)Output:
I LOVE PYTHONString Display Alignment Settings
In Python, we can easily set the justification for the position of the string when it is displayed with the print() command. There are three commands for setting string justification, namely: rjust(), ljust(), and center().
Rjust() Command
The rjust() command is used to set the string in a right-aligned position relative to space which is determined.
Example:
fruit = 'orange'
print(fruit.rjust(20))
print(fruit.rjust(20, '.'))Output:
orange
..............orangeBased on the example above, there are 20 spaces to print the string ‘orange’. In the first example, the position of the string ‘mango’ is at the far right of the space provided and there is a space blank in front of the string. Whereas in the second example, in front of the string there is the character ‘.’.
Ljust() Command
Opposite of rjust(), the ljust() command is used for the left alignment of a string relative to the given space.
Example:
fruit = 'orange'
print(fruit.ljust(20))
print(fruit.ljust(20, '.'))Output:
orange
orange..............Center() Command
The center() command is used to position the string in the middle of the given space.
Example:
fruit = 'MENU'
print(fruit.center(20))
print(fruit.center(20, '*'))Output:
MENU
********MENU********Case Study 9.1
Create a function changeLetter(text, a, b) which will be used to change the letter in parameter a to the letter in b of a text string. The example is:
changeLetter(‘MATEMATIKA’, ‘T’, ‘S’) then the function will return the MASEMASIKA string. In this case, every T in the MATEMATIKA string is changed to S.
instruction:
- First, convert the string into a list
- Change letters using loops
- Use join() to join each element of the list into a string
Another more simple alternative is to use replace()
Answer:
a simple solution to the above case is as follows:
def changeLetter(teks, a, b):
print(teks.replace(a, b))
changeLetter('MATEMATIKA','T','S')Output:
MASEMASIKAThe above program uses the replace() function to convert every T in MATEMATIKA to S.




