String Operations

In the first lesson you learnt about String data type. A String data type holds text value. Value can be enclosed in a single, double or triple quote. Triple quote strings are mostly used to represent strings which span multiple lines.

double_quote_word = "Python"

single_quote_word = 'Python'

multi_line_word = '''First line
  Second line''' # any white spaces are maintained 

There are many operations that you can invoke on a String object.

Here is the list of some of the operations used on a string variable;

word = 'Python'
Operations Example Output Comments
Positive positional index word[0] 'P' positional index starts with 0, prints first character
Negative positional index word[-1] 'n' first position from the end. -0 is same as 0
Slicing word[0:2] 'Py' characters from position 0 (included) to 2 (excluded)
Slicing with default start position word[:2] 'Py' starts from 0 - default and 2nd position (excluded)
Slicing with default end position word[2:] 'thon' starts from position 2 to end of the string
"My " + word + " 3.6"
'My Python 3.6'
You can join multiple strings with a + operator
Build in function len len(word) 6 len is one of the built in functions which is part of the Python interpreter similar to print(). This returns the size of the string

Points to note

  • Strings are immutable - A String once created, cannot be altered in anyway. In all the above operations a new String is returned after applying the operations. Hence assigning a new value to an indexed position results in an error. E.g., word[0] = 'K' is not allowed.
  • Trying to get an indexed position which is out of range results in IndexError. E.g., word[9] will throw IndexError.
  • However slice operations on out of range positions does not thrown IndexError. E.g., word[9:0] gracefully exits without any error and with empty output.

More operations

Code Output Notes
print('he said "Py isn\'t Java\"')
he said "Py isn't Java" note the escape character '\' within isn\'t. This makes sure the single quote is printed as-is instead of throwing an exception
print("First line \n Second line")
First line
Second line
note the new line character \n which breaks the line and prints the part of sentence after \n in the second line
print(r"c:\mydir\numberedDir") c:\mydir\numberedDir note of the use of r to ensure \n part of \numberedDir is not interpreted as a new line character
print( "hi" + " there") hi there using + operator we can concatenate strings
print( 3 * "yah! ") yah! yah! yah! using * we can repeat the string multiple times
text = ("multi line"
" improves readability" )
multi line improves readability
putting strings within parenthesis will join the strings.

Strings are also a type of sequence. There is a built in function str to create strings. str is also used to get textual representation of any object. str is used when you want to represent string using multiple encodings.

Here are a few more commonly used convenience methods on Strings.

Operations Example Output Comments
t = 'py,book,3.6'.split(',')
['py', 'book', '3.6'] returns a list with word split at the specified delimiter (,) (space is used for empty delimiter).
str.splitlines() t = 'py\n3.6'.splitlines()
['py', '3.6'] returns a list with word split on newline
str.capitalize() 'python'.capitalize() 'Python' returns a string with first letter in upper case
str.count(sub[,start[,end]]) 'america'.count('a') 2 counts how many times 'a' occurs in a string
True returns True if the word ends with a specific letter or word else False
str.find(sub[,start[,end]]) 'america'.find('me') 1 returns the index of the first substring which matches the given sub string.
str.lower() 'PYTHON'.lower() 'python' converts the entire string to lowercase
str.upper() 'python'.upper() 'PYTHON' converts the entire string to uppercase

Convert numerical data to String using format

format function is commonly used on numerical data to convert them to strings, for aesthetic reasons in presentations. The table below provides some of the commonly used formats:

Type Example Output Comments
'f' '{:f}'.format(3.141592653589793) '3.141593' using f restricts the output of the decimal portion to 6 digits
'f' '{:06.2f}'.format(3.141592653589793) '003.14' in this example we want the output to be at least 6 characters with 2 decimal places and filling 0 in the beginning
'e' '{:e}'.format(3141592653589793) '3.141593e+15' represents a very large number in exponent notation
'e' '{:10.2e}'.format(3141592653589793) ' 3.14e+15' represents a very large number in exponent notation with at least 10 characters by filling spaces if required.
'd' '{:04d}'.format(52) '0052' decimal number printed with at least 4 characters by adding 0 as required
'd' '{: d}'.format(-52) '-52' use one space to specify - positive numbers should have one space and negative number starts with negative (-) sign
'd' '{:=+4d}'.format(52) '+ 52' specifies the minimum size should be 4 along with displaying + sign for positive numbers
'placeholders' '{x}{y}'.format(x='abc', y='xyz') 'abcxyz' x, and y place holders are replaced with variable values
'placeholders' data = {'first': 'Jane', 'last': 'Doe'}
'{first} {last}'.format(**data)
'Jane Doe' place holders applied to dictionary object
'placeholders' data = {'first': 'Jane', 'last': 'Doe'}
'{p[first]} {p[last]}'.format(p=data)
'Jane Doe' another method for the same output given for convenience
'placeholders' data = {10, 12, 17, 18, 25, 42}
'{d[4]} {d[5]}'.format(d=data)'
25 42 prints out the 5th and 6th element from the list
'placeholders' coord = (5, 8)
'X: {0[0]}; Y: {0[1]}'.format(coord)
'X: 5; Y: 8' prints out the coordinates from the tuple
',' '{:,}'.format(1234567890) '1,234,567,890' using comma for thousands separator

Official reference

results matching ""

    No results matching ""