Python Strings

Strings

Strings are constructed in Python by enclosing letters or numbers within quotation marks. E.g. both "this" and "123" are strings. A string can also be a complete sentence wrapped inside quotation marks "this is also a string".

Single or double quotations can be used but they can't be mixed. E.g. "this" and 'this' are valid but not "this'.

Escape Characters

But what if the word itself contains a comma? Well, I'm glad you asked. In such a case you could simply choose to wrap the string in double quotes:

"This was a tricky one but now it's OK because I have double quotations"

With the inital hurdle jumped, it's on to the next question. What if there are quotations marks within a sentence that also contains a single quotation mark? In such a case you can use the escape character, otherwise know as the backslash character:

"Will said that \"the tricks of the course get progressively deceptive\" that's something I tend to agree with"

The backslash effectively says 'Hey Python, you can ignore the quotation marks or any commas as it isn't the end of the string.

String Concatenation

String concatenation is just a fancy way of saying adding two strings together. If you want to have a space between the words then you need to insert a space either at the end of one string or the beginning of the next:

"Hello" + "World" = "HelloWorld"

"Hello " + "World" == "Hello World"

String concatenation is also useful when you want to add numbers in sequence rather than by value:

"1" + "45" = "145"

 

Important String Methods

There are a lot of different string methods. The following are the methods I have found the most useful to know about as a beginner:

string.lower()  - turns all string elements in a string into lowercase

lower_string = "lowErcasE"

lower_string.lower() = 'lowercase'

string.upper() - turns all string elements into uppercase

upper_string = "UppErcasE"

upper_string.upperer() = 'UPPERCASE'

 

Iterable

Strings are iterable which means that you can you can return each member of a string one at a time. You can use the element's index to do this. An index is the location of the element in the string. Indexes in programming are zero based, they start at 0, this takes a little getting used to but will come more natural to you with practice.

string1 = "hello"

string1[0] will return "h"

 

String Manipulation

The length of a string can be calculated using the len() method.

len(string1) = 5

The count() method counts instances of a character or space in a string:

string1.count('e') = 1

 

String Slicing

Parts of a string can be returned using string slicing. Slicing can take a start and an end index value. The key thing to keep in mind is that the slice will return the starting index and go up to but not including the stop index. This takes a little bit of time, patience and practice to get used to.

The first index is included by using either 0 or a blank space and the last index is included with a blank

string1[:] = "hello"

string1[1:3] = "el"

string1[:-1] = "hell"

 

Join String

The join() method joins a string with an element like a space.

join_string = "Hello World"

comma= ", "

comma.join(join_string) = "H,el,l,o ,W,o,r,l,d"

Strip String

The strip() method returns a copy of the string in which the characters specified at the beginning and end of the string are removed, useful for white space removal:

split_string = " Hello "

split_string.split() = "Hello"

 

Remove All Whitespace

test = "this is a whitespace test"

"".join(test.split()) = "thisisawhitespacetest"

Add Whitespace

test2 = "this is a whitespace test"

"".join(test2.split()) = "this is a whitespace test"

 

Another useful thing to keep in mind are the list of Python keywords that shouldn't be used as variables. A variable is the name you a assign to a value.

e.g.

my_variable = "Done for today"

variable  is my_variable

= is assign symbol

"Done for today" = value

 

 

Are you new to Python? Did you find this blog post useful? Have you any questions? If so I would love to hear from you in the comments section below or by dropping me an e-mail.