Strings are sets of ordered characters that make up text.
Strings are normally contained within
" "
or ' '
.
String tools
Strings can be manipulated in many different ways
such as with other programming languages such as Java. For instance, the
substring
method will get a smaller
inner string from within a string (a substring). In PHP, there are several generic
string methods.
Replace a string inside a string
String replacement is an often used in Python for replacing some substring within another string. In Python, this is a chained function applied to a value:
myStr = "Hello everyone!" myStr = myStr.replace("everyone", "world") print (myStr)
replace
method is called on the string, the value needs
reassigned to myStr
.
Get the length of a string
As strings are arrays of characters, it is possible to get the length of
a string using the len
method:
myStr = "Hello everyone!" print (len(myStr))
Getting the index of the first occurence of a string
Python also provides a find
method that can be applied to strings.
This method will return an integer that represents the index of the first
character of a substring within another string.
myStr = "Hello everyone!" print (myStr.find("Hello")) print (myStr.find("everyone"))
Getting a substring from a string
The word substring has been used throughout this article. It's meaning is a string that is within another string. For instance, "everyone" is within the string "Hello everyone!". The same can be said for "Hello" but not for "hello". This is because substrings are case sensitive.
Python's syntax is different to many other languages in the fact it does not have a substring method. Instead, Python relies on the index of a string using an interesting syntax:
myStr = "Hello everyone!" print (myStr[6:11]) print (myStr[:5]) print (myStr[6:])
The [5:7] following a number means from character 5 to 7 (remembering that Python counts from 0 upwards, not 1). The [:6] index means from the start to 6. The [6:] index means from 6 to the end of the string.
Concatenating two strings
String concatenation is the joining of two or more strings.
Python uses the +
operator for concatenation:
myStr1 = "Hello " myStr2 = "world" concatenated = myStr1 + myStr2 print (concatenated)
Getting a character from a string position
Getting a character from a string in Python is achieved with the same method as getting a substring by using the index of the character:
myStr = "Hello everyone!" print (myStr[4])
The above sample will return the 5th character of the string.
Using quotes inside a string
It is already obvious that since strings in Python can be within a double quote
"
or a single quote '
that they can be interchanged
to include a single or double quote within the output. For example:
print ("Jamie's Code") print ('"' + "Jamie's Code" + '"')
But there is an easier way to do this called escaping. Escaping
is done with the backslash character \
in front of the character to be escaped:
print ('"Jamie\'s Code"') print ("\"Jamie's Code\"")
This is such a useful technique that saves both writing time and compile time.