Strings
String is treated as a basic data type in Python. The string value is always enclosed within (single/double/triple) quotes.
>>> var = "python" # var = 'python'
>>> type(var)
<class 'str'>
The type function shows that the variable var is of type (class) str.
The indexing and slicing in strings is as follows.
Indexing
The forward as well as backward (reverse) indexing is possible with strings. The forward indesing is as follows. The index starts with zero, for example
>>> var = "python" # var = 'python'
>>> var[0]
p
>>> var[5]
n
p y t h o n
forward index 0 1 2 3 4 5
reverse index -6 -5 -4 -3 -2 -1
The reverse/backward indexing starts with the index -1. For example,
>>> var[-1]
n
>>> var[-6]
p
Referring to any out-of-index of any string is a syntax error named as IndexError. For example, the following referencing of the string var results in an IndexError
>>> var[-7] # results in IndexError
>>> var[6] # results in IndexError
The IndexError can be handled using try-except block.
var = 'python'
try:
print(var[6]) # out of index reference
except IndexError:
print("error occured") # error handling code
print("continued execution...") # continue running application
The error IndexError is handled by the code within except block.
string slices
The slice of strings can be fetched as follows.
>>> var = 'python'
>>> var[0:3] # from index 0 to 2
'pyt'
>>> var[2:] # from index 2 to end
'thon'
>>> var[:4] # from index 0 to 3
'pyth'
>>> var[:] # entire string
'python'
String slices can also be obtained using reverse indexing. Some examples are as follows.
>>> var[-1:-4] # This is not the way to get a substring
''
>>> var[-6:-3] # Yes. This is the way. start=-6, end=-3-1=-4
'pyt'
>>> var[:-2] # from the start of the string to (-2-1 = -3)
'pyth'
>>> var[-2:] # from -2 to the end of the string
'on'
String slice can also be using the combination of positive and negative indexing. Some examples are as follows.
>>> var[-10:-3] # substring is from -6 to -4
'pyt'
>>> var[5:10] # last character index is 5 and up to 10 nothing
'n'
>>> var[-6:4] # start = -6 and stop = 4-1=3
'pyth'
>>> var[0:-1] # start=0 and stop=-1-1=-2
'pytho'
Out-of-index in slices: Out-of-indexing in slices always results in an empty string. Some examples are as follows.
>>> var[-14:-7] # start = -14, stop = -7-1=-8
''
>>> var[8:10]
''
Finding a substring in strings ( in and not in)
The in and not in operators are useful to verify the existence of substrings in strings. The following examples illustrate the same.
>>> var = "Python Programming Language"
>>> "Python" in var # substrin 'Python' present in var
True
>>> 'python' in var # substring 'python' not present (lower case p)
False
Same thing can be achieved using not in operator
>>> var = "Python Programming Language"
>>> "Python" not in var # substring 'Python' present in var
False
>>> 'python' not in var # substring 'python' not present (lower case p) in var
True
Similarly
>>> 'Py' in 'Python'
True
>>> 'Jy' not in 'Python'
True
Strings with single, double and triple quotes
Double quote strings are useful to include single quotes as part of the string and vice versa. The following examples illustrate the same.
>>> var = "string's" # single quote is part of the string
>>> var
"string's"
>>> var = 'string "and" str' # double quote is part of the string
>>> var
'string "and" str'
The escape sequence is another way to use quotes as a part of the string. Some of the escape sequences are as follows.
_____________________________
Escape character print as
_____________________________
\' '
\" "
\\ \
\t tab
\n newline
____________________________
Some examples using escape sequence is as follows
>>> var = 'string\'s' # \' is replaced by '
>>> var
'string's
>> var = "string \"and\" string" # \" is replaced by "
>>> var
string "and" string
Triple quotes are used to enclose the string of multiple lines with some text format. The following example illustrates the same.
>>> var = """ Subject marks
C 45
Java 55
Python 65
"""
>>> print(var)
""" Subject marks
C 45
Java 55
Python 65
"""
The triple quote strings are also used as multi-line comments in Python programs.
Strings are immutable
Strings, once created, cannot be modified. They are immutable in nature. For example
>>> var = "Python'
>>> var[0] = 'J' # results in TypeError
String (type str) objects do not support item assignment. But, it is possible to create new strings from existing strings. For example,
>>> new_var = 'J' + var[1:] # 'J' + 'ython' = 'Jython'
>>> new_var
'Jython'
New string named as new_var is created using the existing string var.
String methods
There are a handful of methods available to analyze strings and to create new stings, substrings, and slices from the existing strings. Remember that the strings immutable, once created cannot be modified. Let us study them.
Methods for case conversion and verification in strings
The methods for case conversion and verfications are as follows.
lower(): This method coverts the alphbets of the string into lower case.
>>> var = 'PYTHON'
>>> var.lower() # var string is converted into lowercase
'python'
>>> var # remember that the strings are immutable
'PYTHON'
The preceding code shows that the the string value of the variable var is not changed. If the changed value is need for further reference, assgin the converted string into another string variable. This is as shown here.
>>> new_var = var.lower()
>>> new_var
'python'
This is also possible to assign the newly created (converted into lower case) string to the same string variable.
>>> var = var.lower() # new string is assigned to var
>>> var
'python'
Similarly, the upper() method in strings also works.
upper(): This method converts the string into uppercase. It is used as follows.
>>> 'java'.upper()
'JAVA'
>>> var = 'c++'
>>> var.upper()
'C++'
isupper() and islower(): These two methods are useful to verify the case of the strings. The following code snippet illustrate the same.
>>> 'python123'.islower() # all alphabets are lowercase
True
>>> 'Python'.islower() # 'P' is uppercase
False
Similarly, the isupper() function also used to verify the case of strings.
>>> 'JAVA'.isupper() # all alphabets are in uppercase
True
>>> var = 'Java'
>>> var.isupper() # there are lower case alphabets in var
False
All these methods return string. These methods can be called in chain as shown next.
>>> var ='python'
>>> var.upper().lower().upper() # 'python -> 'PYTHON' -> 'python' -> 'PYTHON'
The evaluation is from left to right. The result of the last method (right extreme) is returned.
Methods in the String class to verify alphanumeric in string objects
There are also methods available to verify the string to observe the numeric digits. They are as follows.
isalpha() # is a string that contains only letters (a-z, A-Z)
isalnum() # is a string value that is alphanumeric
isdecimal() # is string value is decimal
isspace() # is a string that contains only spaces
istitle() # is the string obeys the upper and lower case rules for titles
>>> 'pYthon'.isalpha() # only letters
True
>>> 'pyTHon123'.isalnum() # letters and numbers
True
>>> '12321'.isdecimal() # only numbers
True
>>>'44.53'.isdecimal() # only digits allowed
False
>>> 'Python Programming Language'.istitle() # First letter of each word is uppercase
True
String beginning (startswith()) and ending (endswith())
The startswith() and endswith() functions of the String class are used to verify whether the string object starts and ends with a particular substring respectively. The following code lines illustrate these two functions.
>>> var = 'Hello world'
>>> var.startswith('hello')
True
>>> var.endswith('world')
True
Also
>>> 'Python'.startswith('Python')
True
>>> 'Python'.endswith('Python')
True
>>> 'Python'.endswith('ho')
False
List to string conversion (join()) and string to list conversation (split())
The following code lines help you to understand the conversion from string to list elements.
>>> var = 'Python Programming Language'
>>> lt = var.split()
>>> print(lt)
['Python', 'Programming', 'Language']
By default, the split is based on white space. The coder can specify the split basis as an argument of the split function. In the following example, the split basis is the newline character.
>>> var = '''Python # multiline string
Programming
Language'''
>>> lt = var.split('\n')
>> print(lt)
['Python', 'Programming', 'Language']
The opposite is conversion from a list to a string using the function join(). The following example illustrates the same.
>>> lt = ['Java', 'Programming', 'Language']
>>> string_obj = '\n'.join(lt)
>>>print(string_obj)
Java
Programming
Language
The elements of the strings can be joined using a blank space as follows.
>>> string_obj = ' '.join(lt)
>>> print(string_obj)
Java Programming Language
Formatting string contents using ljust(), rjust() and center() functions
String class has some functions for formatting the string content. These functions are useful to display the results with improved aesthetic.
ljust() -> This function is used to justify the content to the left.
rjust() -> This function is used to justify the content to the right.
center() -> This function is used to center the content of the string
The following examples illustrate the same.
>>> var = 'Python'
>>> var.ljust(10) # left justify, remaining 4 characters are blank to the right
'Python '
>>> var.rjust(10) # right justify, remaining 4 characters are blank to the left
' Python'
>>> var.center(10) # center content; 2 characters are blank on both sides
' Python '
>>> var.center(10, *) # can fill blank space with specific character
'**Python**'
Functions to remove white space from strings
The following String class functions are useful to remove the whitespace from strings.
lstrip() -> removes left-side whitespace of the strings
rstrip() -> removes right-side whitespace of the strings
strip() -> removes both sides whitespace of the strings
The following code lines illustrate the same.
>>> var = ' Python '
>>> var.lstrip()
'Python '
>>> var.rstrip()
' Python'
>>> var.strip()
'Python'
This is much beyond simple striping. Both side striping characters can be passed as an argument
>>> 'string'.lstrip('st')
'ring'
>>> 'python'.rstrip('on')
'pyth'
>>> 'python'.strip('pyon') # strips 'py' from left and 'on' from right side
'th'
>>> var = 'StringstringString'
>>> var.strip('Stgn')
'ringstringStri'
Copy and paste through clipboard of computer
The Python module referred to as pyperclip has copy and paste functions to use the clipboard of the computer. You can copy the string from one place to the clipboard and paste it into another place using these functions. The following code line illustrates the same.
>>> import pyperclip
>>> pyperclip.copy('Python Programming Language') # copied to the clipboard
>>> pyperclip.paste() # pasted onto the screen
'Python Programming Language'
No comments:
Post a Comment