28.Strings in Python

Strings are compound data type made of sequence of characters. Hence from the string, the individual charters can also be directly accessed. Strings are created by using single quotes or double quotes or triple quotes.
Example:
>>>s1="Python Programming"
>>>s2='Python Programs'
>>>s3="""Python is a powerful
Programming language"""

This will create a string object s1, s2 and s3 using different forms.Individual characters can be accessed using different subscripts( +ve or –ve) with the string object.
The positions of string's characters are numbered from 0.So the 5th character is in position 4.

When working with strings, the programmer sometimes must be aware of a string's length.Python len() function can be used to find the number of characters in the string
Eg:
>>> len('python')
6
>>> len("")
0
Note: The string is an immutable data structure.This means that its internal characters can be accessed, but cannot be replaced, inserted or removed.

Subscript Operator

Although a simple for loop can access any of the characters in a string, some times you just want to inspect one character at a given position without visiting them all.The subscript operator [ ] makes this possible.The simplest form of subscript operator is the following

string[ <an int expression>]

The first part is the string you want to inspect. The integer expression in brackets indicates the position of a particular  character in that string. The integer expression is also called index.
Examples:
>>>s="Python"
>>>s[0]
‘P’
>>>s[2]
‘t’
>>>s[5]
‘n’
we can also use negative index. -1 is the index of last character.
>>>s[-1]
‘n’
>>>s[-3]
‘h’
>>>s[-6]
‘P’
Note that the position ranges from 0 to len(string)-1.Invalid index will generate IndexError.
The subscript operator is also useful in loops where you want to use the positions as well as the characters in a string
 
for i in range(len(s)):
 print(i,s[i])
0 P
1 y
2 t
3 h
4 o
5 n
>>>s[2]=’c’ will leads to an error because strings are immutable.

Slicing for substrings

We can extract a portion of string called substring using a process called slicing.To extract substring, a colon(: ) is placed in the subscript.
Generally when two integer positions are included in the slice, the range of characters in the substring extends from the first position up to but not including the second position.
>>>s[1:3]
‘yt’
If the first index is not mentioned, the slicing will start from 0.
>>>s[:3]
‘Pyt’
>>>s[2:6]
‘thon’
If the last index is not mentioned, the slicing will go till the end of the string.
>>>s[3:]
‘hon’
We can also specify the step size in slicing. The example below slice 0,2 and 4th character.
>>>s[0:5:2]
'Pto'
We can also use –ve index in slicing
>>>s[1:-1]
'ytho'
>>> s[-4:-1]
'tho'
>>> s[-6:-2:2]
'Pt'
Negative step size makes string to be printed in reverse
>>> s[5:0:-1]
'nohty'
>>>s[-1:-7:-1]
'nohtyP'
Print the full string
>>> s[:] or >>>s[::]
'Python'
Print the string in reverse
>>> s[::-1]
'nohtyP'
#Write the output of following python code : ( University Question)
S ="Computer"
print(S[::2])
print(S[::-1])
print(S[:])

output:
Cmue 
retupmoC 
Computer

String Operations
The following operators can be applied to string apart from slicing.

+ operation can be used as concatenation operation.
>>>s1=”Python”
>>>s2=”Programming”
>>>s3=s1+s2
>>>s3
‘Python Programming’

‘*’ operation repeats a string specified number of times
>>>s4=s1*3 or >>>s4=3*s1
>>>s4
‘PythonPythonPython’

“in” and “not in” operator can be used to check whether a substring is present in another string
>>>”Py” in s1
True
>>>”Py” not in s1
False

String Methods
Text processing involves many different operations on strings.The string methods can be used to do different operations on strings. A method is like a function, but it runs "on" an object. If the variable ‘s’ is a string, then the code s.lower() runs the lower() method on that string object and returns the result (this idea of a method running on an object is one of the basic ideas that make up Object Oriented Programming, (OOP).The built in functions like len(),min(),max() etc can be used directly on string object. You can import string module and use string methods or with a string object call these functions.

Here are some of the most common string methods:
Let s is Python string then s.lower() and s.upper() will return the lowercase and uppercase versions of the string s.islower() and s.isupper() can be used to test whether s is lower case or upper case.
Example
>>>s=”Python programming”
>>>s.upper()
PYTHON PROGRAMMING
>>>s.lower()
python programming
>>>"binu".islower()
True
>>>"Binu".islower()
False
>>>"BINU".isupper()
True
>>>"BINu".islower()
False
s.isalpha(),s.isdigit(),s.isalnum() s.isspace() will test for alphabets, digits, alpha numeric and space.
>>> "abc".isalpha()
True
>>> "ab1".isalpha()
False
>>> "123".isdigit()
True
>>> "ab1".isdigit()
False
>>> "a12".isalnum()
True
>>> "a+".isalnum()
False
>>> " ".isspace()
True
>>> "abc".isspace()
False
s.find() and s.index() can be used to find a substring in the given string.It returns the position(index) of the first occurrence of the substring. If it is not present it will return -1 in case of find() method and throws an error in case of index() method.We can also specify the start and end position of the string index so that the search will be done in this range only.We can also use rfind() to search for a substring from the right end of the string.
>>>s=”Python programming”
>>>s.find(‘pr’)
7
>>>s.find(‘to’)
-1
>>> s.find('o')
4
>>> s.rfind('o')
9
>>>s.find(‘pr’,8,-1)
-1
>>> s.find('ton')
-1
>>> s.index('ton')
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
s.index('ton')
ValueError: substring not found
>>>s.index(‘on’)
4
s.count() can be used to count the number of occurrence of a substring.
>>>s.count(‘m’)
2
>>>s.count(‘mii’)
0
s.title() converts into a title my making first character of each word capital.
s.istitle() can be used to test whether the string is title cased.
>>>s.title()
‘Python Programming’
>>>s.istitle()
False
s.capitalize() Return a copy of the string s with only its first character capitalized.The remaining characters are turned into small case.
>>>"hello MAN".capitalize()
'Hello man'
s.swapcase() will swap the case of each character.(upper case into lower case and vice versa)
>>> s.swapcase()
'pYTHON pROGRAMING'

s.replace(old,new) will replace all the occurrence of old substring with new
>>> s.replace('m',"M")
'Python PrograMMing'

s.split(char) will take a character and split s based on char. This function can be used to split a string into words with space as split character.
>>> s.split(" ")
['Python', 'Programming']
>>> s.split("n")
['Pytho', ' Programmi', 'g']

The partition() function will partition the string into 3 parts. They are, string before partition string, the partition string and the string after partition string.
 >>> s="this is a test"
>>> s.partition("is")
('th', 'is', ' is a test')

The lstrip() and rstrip() finctions can be used to strip white spaces or a substring from left or right end. strip() method is used to do the stripping from both the ends. When no substring is specified, white spaces are striped.
>>> s="this is a test"
>>> s.lstrip('th')
'is is a test'
>>> s.rstrip('st')
'this is a te'
>>> s.strip('t')
'his is a tes'

join() method joins the string elements with a separator.
>>> ":".join(s)
'P:y:t:h:o:n: :P:r:o:g:r:a:m:m:i:n:g'
This will create a reversed string
>>>''.join(reversed(s))

We can also use advanced functions for string translations
intab = "aeiou"
outtab = "12345"
str = "this is string example....wow!!!"
trantab = str.maketrans(intab, outtab)
print(str.translate(trantab))
This will do a translation as per the table
th3s 3s str3ng 2x1mpl2....w4w!!!

String constants
The following are some of the string constants in string module.If you want to use it import string module.
Example
>>>import string
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.digits
'0123456789'
>>>string.hexdigits
'0123456789abcdefABCDEF'
>>> string.octdigits
'01234567'
>>> string.punctuations
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}-'
>>> string.whitespace
'\t\n\x0b\x0c\r '
>>> string.printable
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!
"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}- \t\n\r\x0b\x0c'

sample program using string constant
This program will convert lowercase letters into uppercase and uppercase letters into lowercase.
import string
s=input("Enter a String ")
ns=""
for i in s:
    if i in string.ascii_lowercase:
        ns=ns+i.upper()
    elif i in string.ascii_uppercase:
        ns=ns+i.lower()
    else:
        ns=ns+i
print ("New String=", ns)

Note:
Regular strings in python are byte strings. For creating a Unicode string use the prefix ‘u’ on the string literal.
>>>ustring = u'A Unicode string’
A raw string which does not consider the special characters uses the prefix r.
>>>s=r’python \t python’

To know more about string module functions refer  https://www.tutorialspoint.com/python/python_strings.htm


>>> s = "Hi there!"
>>> len(s)
9
>>> s.center(11)
' Hi there! '
>>> s.count('e')
2
>>> s.endswith("there!")
True
>>> s.startswith("Hi")
True
>>> s.find("the")
3
>>> s.isalpha()
False
>>> 'abc'.isalpha()
True
>>> "326".isdigit()
True
>>> words = s.split()
>>> words
['Hi', 'there!']
>>> " ".join(words)
'Hithere!'
>>> " ". join(words)
'Hi there!'
>>> s.lower()
'hi there!'
>>> s.upper()
'HI THERE!'
>>> s.replace('i', 'o')
'Ho there!'
>>> " Hi there! ".strip()
'Hi there!'

Comments

Popular posts from this blog

Python For Machine Learning - CST 283 - KTU Minor Notes- Dr Binu V P

KTU Python for machine learning Sample Question Paper and Answer Key Dec 2020

46.Classes and Objects in Python- Accessors and mutators