what is String function in python 3.10


In python there are more than 40 inbuilt string methods, detailed explanation of some of the following

are given below.

String function in python
String function in python

inbuilt string methods in python 3.10

1. Split()- Being a competitive coder, this function is one of the most used function in my coding carrier.

This is a very simple and very important at the same time. This function helps in splitting a string with

help of a delimiter.

Ex:

s = "hello there"

s.split(' ')

output: ['hello', 'there']

2. format() – This is also a very important string function. It helps us to format any string. We give some

variables in {} and specify their value at the end of the string. It helps us in creation of dynamic strings.

Ex: txt1 = "My name is {fname}, I'm {age}".format(fname = "swapnil", age = 21)

Output: My name is swapnil, I'm 21

3. upper() and lower()– These two functions helps us to convert a string into uppercase or lowercase.

Str.upper() converts a string in uppercase and str.lower() converts the string into lowercase.

Ex:

s = 'Hi i am Swapnil'

print(s.upper())

print(s.lower())

output:

HI I AM SWAPNIL

hi i am swapnil

4. find()– This is also one of the most used functions and helps us to find if a sub string is present inside

a string or not.

This function returns starting index of the substring if the substring is present, else returns -1.

Ex:

s = 'Hi i am Swapnil'

print(s.find('Swapnil'))

print(s.find('hello'))

output:

8

-1

5. replace()– This function helps us to replace any substring of a string with any desired new string. It

takes old substring which is to be replaces and new string as its parameter.

Ex:

s = 'Hi i am Swapnil'

s.replace('Hi','hello')

output:

hello i am Swapnil

6. isalpha()– This function returns true if all the charecters in a given strings consist of alphabets,

otherwise returns false.

Ex:

s = 'apple'

print(s.isalpha())

s='1'

print(s.isalpha())

output:

True

False

7. isdigit()– This function returns true if all the charecters in a given strings consist of digits, otherwise

returns false.

Ex:

s = 'apple'

print(s.isalpha())

s='1'

print(s.isalpha())

output:

False

True

8. count()– This function returns count of any substring given as a parameter in this function.

Ex:

s = 'apple'

print(s.count('p'))

Output:

2

9. endswith() and startswith()– These functions checks if the string starts or ends with a substring

given as a parameter in these functions.

Ex:

s = 'apple'

print(s.startswith('a'))

print(s.endswith('e'))

Output:

True

True

10. strip()– This function helps in removing all the extra whitespaces from any string.

Ex:

s = '              apple              '

s.strip(' ')

Output:

Apple

Summary :

In this article we saw what is String function in python 3.10 so about this section you have any query then free to ask me

Name of Intern who share this Task

swapnil raj

I am Mr. Sachin pagar the founder of Pythonslearning, a Passionate Educational Blogger and Author, who love to share the informative content on educational resources.

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

google ads

Pythonslearning protected By DMCA

NEWS ADS