0% found this document useful (0 votes)
1 views5 pages

Practical -String Functions

Uploaded by

Jesica D'cruz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
1 views5 pages

Practical -String Functions

Uploaded by

Jesica D'cruz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

String Operators

Assume string variable a holds 'Hello' and variable b holds


‘ComputerScience' , then−

Operator Description Example


+ Concatenation-Adds values on a + b will give
either side of the operator HelloComputerScience

* Repetition – Creates new strings, a*2 will give -HelloHello


concatenating multiple copies of
the same string

[] Slice-Gives the character from a[1] will give e


the given index

[ :] Range Slice – Gives the a[1:4] will give ell


characters from the given range

In Membership- Returns True if a H in a will be true


character exists in the given
string

not in Membership - Returnstrue if a M not inawill givetrue


character does not exist in the
givenstring

String Functions
1. capitalize()-
str1="hello python"
>>> str1.capitalize()
'Hello python'
>>> str1
'hello python'
>>> str2=str1.capitalize()
>>> str2
'Hello python'
>>>
2. center()
str1.center(30)
' hello python '
3. count() -counts the no of occurrences of a particular in the string
>>> subject="Python Programming"
>>> subject.count("r")
2
4. find()

• The find() method finds the first occurrence of the specified value.

• The find() method returns -1 if the value is not found.

• The find() method is almost the same as the index() method, the
only difference is that the index() method raises an exception if the
value is not found.

subject="Python Programming"

subject.find("on")

>>> subject.find("no")

-1

5. index()

• The index() method finds the first occurrence of the specified


value.

• The index() method raises an exception if the value is not found.

subject="Python Programming"

subject.index("on")
4
>>> subject.index("no")
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
subject.index("no")
ValueError: substring not found
5. replace()
The replace() method replaces a specified phrase with another
specified phrase.
Syntax: string.replace(oldvalue, newvalue, count)

Parameter Description

Oldvalue Required. The string to search for

Newvalue Required. The string to replace the old value with

Count Optional. A number specifying how many occurrences of the old value you
want to replace. Default is all occurrences

subject.replace("Python","Java")
'Java Programming'

6. split()
Split a string as per separator into a list where each word is a list item
You can specify the separator, default separator is any whitespace.

string.split(separator, maxsplit)

Parameter Description

Separator Optional. Specifies the separator to use when splitting


the string. By default any whitespace is a separator
Maxsplit Optional. Specifies how many splits to do. Default
value is -1, which is "all occurrences"

str="abc,def,ghi,jkl"
>>> str.split(",")
['abc', 'def', 'ghi', 'jkl']
7. join()

• The join() method takes all items in an iterable and joins them
into one string.

• A string must be specified as the separator.

list1=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"
,"Saturday"]
>>> str1="#".join(list1)
>>> str1
'Sunday#Monday#Tuesday#Wednesday#Thursday#Friday#Saturday'

8. strip()
The strip() method removes any leading (spaces at the beginning)
and trailing (spaces at the end) characters (space is the default leading
character to remove)
str1=" abc "
>>> str1=str1.strip()
>>> str1
'abc'
9. upper()- Converts a string into upper case
10.lower()-Converts a string into lower case
11.title()-Converts a string into title case
12. isalnum()
Returns True if all characters in the string are alphanumeric
13. isalpha()
Returns True if all characters in the string are in the alphabet
14. isdecimal()
Returns True if all characters in the string are decimals
15. isdigit()
Returns True if all characters in the string are digits
16. isidentifier()
Returns True if the string is an identifier
17. islower()
Returns True if all characters in the string are lower case
18. isnumeric()
Returns True if all characters in the string are numeric
19. isprintable()
Returns True if all characters in the string are printable
20. isspace()
Returns True if all characters in the string are whitespaces
21. istitle()
Returns True if the string follows the rules of a title
22. isupper()
Returns True if all characters in the string are upper case

You might also like