The 5 basic but most error-prone cases for String in Python
By: Xue Wang
Tags: String, Python

Photo by Paul Esch-Laurent on Unsplash

A string is a sequence of characters. It is a basic data type in Python, but do you really know how to use it? Let’s discuss the most 5 error-prone cases as below.

1. Single quotes, double-quote, or triple quotes

For example, if you want to output the string: Tom said: “How are you? I’m fine too.”, let’s check different methods:

Method1: using single quotes

The code and the result will be:

Method2: using double quotes

The code and the result will be:

Method3: using triple quotes

Method4: using escape method together single quotes

Method5: using print function together triple quotes

Method6: using print together triple quotes for multiple lines

Do you find the conclusion? So to show the sentence which includes quotes correctly, the following tips can be used:

If the string has only single quotes, then double quotes can be used

If it has only double quotes, single quotes can be used

If the string has both single and double quotes or the string has multiple lines, better use triple quotation

Using the print function can avoid escape characters compared with direct output string

2. len(string) or len(string)-1: index or slice

If there is a string = ‘8euro/piece’, you want to

get the last character of the string

get the last five characters of the string

Let’s try different methods to see what the results are:

string2 = '8euro/piece'

Output1:

Output2:

Output3:

Output4:

From the results, we know output 2 is for question 1, and output 4 is for question 2. This is because:

Request 1: using an index. Although len() gives the total number of characters of a string, since it starts with 0, the index for the last character is len()-1.

Request2: using the method of slicing. Slicing syntax is S[firstIndex: tailIndex] to get the characters from firstIndex to tailIndex -1

3. int, float, or int(float)

There are two strings:

string3 = '8.8euro/piece'

How to get the integer part from the number of string3(‘8.8’)?

How to round out the number of string3(‘8.8’)?

Let’s have a try:

Test1:

Test2:

Test3:

The tip is:

int(): only transfer to integer type from:

float

the integer part from a string

If you want to get the integer part of the string 8.8, first transfer it into float type, then use int().

4. bool(): True or False

Let’s summarize the cases as below:

print("''is",bool(''))

print("space' ' is",bool(' '))

print('None is',bool(None))

print('True is', bool(True))

print('Number 0 is', bool(0))

print('string 0 is',bool('0'))

The output is:

using bool() together len():

This method can be used as a logical condition with if.

So the basic rule is:

For bool(): if the passed value is None, empty or 0 (number, not string 0), it returns False, otherwise True.

5. .join() methode

When using BeautilfulSoup to extract data from a website, normally the data will be saved as a list, then using for loop to read every element. Take an example as :

list1=['The Shawshank Redemption','(1994)','9.6']

To save it to a CSV file, it needs to be saved as a string. join() method works here.

The String is immutable, so you cannot directly delete or insert specific characters between strings. To do that, the string needs to be converted into a list, then use .insert(), combing with .join() method, the example as below:

Today, we try the codes for the most error-prone cases for String in Python. Do you get a more clear idea of the string operations? Have fun!