‘Python Programming- Strings explained’ post is follow up to Python Programming Tutorial- Expressions and Variables post. All posts in this series are based on course “Python For Data Science” at cognitiveclass.ai.
Watch the Video
Python has a built-in string class named “str” with many handy features. A ‘string’ is simply a list of characters in order. A character is anything you can type on the keyboard in one keystroke, like a letter, a number, or a backslash.
String literals can be enclosed by either double or single quotes, although single quotes are more commonly used. Backslash escapes work the usual way within both single and double quoted literals — e.g. \n \’ \”.
A double quoted string literal can contain single quotes without any fuss (e.g. “I didn’t do it”) and likewise single quoted string can contain double quotes.
A string literal can span multiple lines, but there must be a backslash \ at the end of each line to escape the newline. String literals inside triple quotes, “””” or ”’, can span multiple lines of text.
There are three ways you can declare a string in Python: single quotes (
'), double quotes (
"), and triple quotes (
"""). In all cases, you start and end the string with your chosen string declaration. For example:
>>> print ('I am a single quoted string') I am a single quoted string >>> print ("I am a double quoted string") I am a double quoted string >>> print ("""I am a triple quoted string""") I am a triple quoted string
A string can be spaces, digits: ‘1 2 3 4 5 6 ‘
A String can also be special characters: ‘@#2_#]&*^%$’
Indexing of Strings
its is helpful to think of a string as an ordered sequence. each element in the sequence can be accessed using an index represented by the array of numbers.
the first index can be accessed as follows: Name
print(Name) can be used to 14th character.
Check for Negative index print(Name[-15])
The standard zero-based index numbers give easy access to chars near the start of the string. As an alternative, Python uses negative numbers to give easy access to the chars at the end of the string: s[-1] is the last char ‘o’, s[-2] is ‘l’ the next-to-last char, and so on.
Negative index numbers count back from the end of the string:
- s[-1] is ‘o’ — last char (1st from the end)
- s[-4] is ‘e’ — 4th from the end
- s[:-3] is ‘He’ — going up to but not including the last 3 chars.
- s[-3:] is ‘llo’ — starting with the 3rd char from the end and extending to the end of the string.
It is a neat truism of slices that for any index n,
s[:n] + s[n:] == s. This works even for n negative or out of bounds.
Or put another way s[:n] and s[n:] always partition the string into two string parts, conserving all the characters.
Another Example- Let us have a Handy string ‘Life is Hard but still enjoyable!’
The “slice” syntax is a handy way to refer to sub-parts of sequences — typically strings and lists. The slice s[start:end] is the elements beginning at start and extending up to but not including end. Suppose we have s = “Hello”
- s[1:4] is ‘ell’ — chars starting at index 1 and extending up to but not including index 4
- s[1:] is ‘ello’ — omitting either index defaults to the start or end of the string
- s[:] is ‘Hello’ — omitting both always gives us a copy of the whole thing (this is the pythonic way to copy a sequence like a string or list)
- s[1:100] is ‘ello’ — an index that is too big is truncated down to the string length.
Thanks to Google’s Python Course.
Quick overview of basic operations:
print(): Prints its parameter to the console.
raw_input(): asks the user for a response, and returns that response. (Note that in version 3.x
raw_input()does not exist and has been replaced by
len(): returns the length of a string (number of characters)
str(): returns the string representation of an object
int(): given a string or number, returns an integer
There are many string operation methods in Python that can be used to manipulate the data. We are going to use some basic string operations on the data.
The method upper; this method converts upper case characters to lower case characters
A=”Thriller is the sixth studio album”
How to Search for Substrings
string.find(s, sub[, start[, end]])
Return the lowest index in s where the substring sub is found such that sub is wholly contained in s[start:end]. Return -1 on failure.
Defaults for startand end and interpretation of negative values is the same as for slices.