String Functions Class11

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

String functions

• Python offers many built-in functions and methods for string


manipulation.

1. len() function:
It returns the length of a string.
Syntax: len(<string_name>)
Eg: name = “Hello”
print(len(name)) returns 5

Q. Write a program to accept a string and display the number of


characters in the string.
2. capitalize() method:
• It returns a copy of string with first letter capitalized.
• Syntax: <stringname>.capitalize()
• Eg: name = “tom”
name.capitalize() returns Tom

3. count() method:
It returns the number of occurrence of a substring in a string or string slice.
Syntax: <string>.count(sub,start,end)
Eg: ‘abcdabacab’.count(‘ab’)
‘abcdabacab’.count(‘ab’,4,8)
‘abcdabacab’.count(‘ab’,4)
4. find() method:
Returns the lowest index in the string where a substring is found.
Returns -1 if not found.
Syntax: <string>.find(sub, start,end)
Eg: ‘abcdaba’.find(‘ab’) returns 0

5. index() method:
Returns the lowest index in the string where a substring is found.
Results in ValueError if not found.
‘abcdaba’.index(‘ab’) returns 0
‘abcdaba’.index(‘ab’,2,5) ValueError
6. isalnum()method:
- Returns True if characters in a string are alphanumeric (alphabet or
number)
- Space character is not an alphanumeric.
Eg: ch = ‘abcde’
ch.isalnum() returns True.

7. isalpha() method:
Returns True if characters in a string are alphabets only.
Returns False otherwise.
‘abc123’.isalpha() returns False
‘abcd’.isalpha() returns True
8. isdigit() method:
Returns True if all characters in a string are digits only, otherwise
returns False.
Eg: ‘abcd123’.isdigit() returns False
‘1234’.isdigit() returns True

9. islower() method:
Returns True if all the characters in a string are of lowercase, other
wise returns False.
Eg: ‘abcd’.islower() returns True
‘Abcd’.islower() returns False
10. isupper() method:
Returns True if all the characters in the string are of uppercase.
Eg: ‘abcd’.isupper() returns False
‘ABCD’.isupper() returns True.

11.isspace() method:
Returns True if the string contain only space “ ”.
“ ”.isspace() returns True
“”.isspace() returns False since no space.
• 12. upper()
Converts all the characters in a string to uppercase.
Eg: ‘hello’.upper() returns ‘HELLO’
ch = “world”
ch.upper() returns ‘WORLD’

13. lower()
Converts all the characters in a string to lower case.
Eg: ‘HELLO’.lower() returns ‘hello’
‘World’.lower() returns ‘world’
14. lstrip(),rstrip(),strip()
lstrip() - Whitespaces from leftmost end are removed.
Eg: “ abcd ”.lstrip() returnd “abcd ”

rstrip() - Whitespaces from rightmost end are removed.


Eg: “ abcd ”.rstrip() returns “ abcd”

strip() – removes whitespace from leading and trailing end.


Eg: “ abcd ”.strip() returns “abcd”
15. startswith(), endswith()
startswith() returns True if a string start with a mentioned substring.
Eg: “abcdababcd”.startswith(“ab”) True
“abcdababcd”.startswith(“cd”) False

endswith() returns True if a string ends with a mentioned substring.


Eg: “abcdababcd”.endswith(“ab”) False
“abcdababcd”.endswith(“cd”) True
16. title() method:
Returns the string where all the words start with uppercase characters
and all remaining characters are in lowercase.
Eg: “hello world”.title() returns “Hello World”
“HELLO WORLD”.title() returns “Hello World”

17. istitle() method:


Returns true if a string is in title format.
“Hello World”.istitle() returns True
“HELLO WORLD”.istitle() returns False
18. replace() method:
Returns a string by replacing an old substring with new substring.
Syntax: <string>.replace(old, new)
Eg: “abcdababacd”.replace(“cd”, “ab”) returns “ababababaab”

19. join() method:


Syntax: <string>.join(<string iterable>)
Eg: “*”.join(“hello”) returns ‘h*e*l*l*o.
A character/string is joined with each character/element of a given
string/sequence.
Eg: “***”.join([“hello”, “world”]) returns ‘hello***world’
20. split() method:
Splits a string based on a given character/string and returns a list
containing split string as members.
Eg: “I love Python”.split(“ ”) Returns [‘I’, ‘love’, ‘Python’]
“I Love python”.split(“o”) returns [‘I L’, ‘ve pyth’, ‘n’]

21. partition() method:


This method splits a string at the first occurrence of a separator string,
and returns a tuple containing 3 items:
- Part of string before separator
- Seperator
- Part after separator.
Eg: txt = “I enjoy working with python”
txt.partition(“working”)
returns a tuple (“I enjoy”, “ working”, “with python”)

Q. Write a program to accept a string , and display the count of


uppercase letter, lowercase letters, digits, alphabets and special
characters.

Q. Write a program to accept a decimal number and display the


fractional part of the number.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy