string functions in python 3 with examples

Preview

string functions in python 3

1. String .capitalize()

This method makes first character of string capital.

Example

>>> name=’lovejot’

>>> name.capitalize()
‘Lovejot’

#First character becomes capital




 2. String .find()

This method is used to find first index position of substring in a string value. It returns-1 if substring is not found.

Syntax:

String.find (Substr[,Start[,End]])

Substr refers to substring we want to find in String.

Start refers to starting index.

End refers to ending index of range in which we have to find substring.

 Example

>>> name=’lovejot’

>>> name.find(‘o’)
1

#Index of first occurance of ‘o’ is shown i.e. 1.

>>> name.find(‘o’,2,7)
5

#Index of ‘o’ in index range 2,7 is shown i.e. 5.

3. String .isalnum()

This method return True if string contains alphabets or digits. There must be at least one character in String.

Example

>>> name=’lovejot’

>>> name.isalnum()
True

>>> name1=’lovejot2019′

>>> name1.isalnum()
True

4. String .isalpha()

This method return True if string contains alphabets only. There must be at least one character in String.

Example

>>> name=’lovejot’

>>> name.isalpha()
True

>>> name1=’lovejot2019′

>>> name1.isalpha()
False



5. String .isdigit()

This method return True if string contains digits only. There must be at least one character in String.

Example

>>> name=’123′

>>> name.isdigit()
True

>>> name1=’lovejot2019′

>>> name1.isdigit()
False

6. String .islower()

This method return True if string contains lower case alphabets only. There must be at least one character in String.

Example

>>> name=’lovejot’

>>> name.islower()
True

>>> name1=’Lovejot’

>>> name1.islower()
False

#First character is capital in ‘Lovejot’.

7. String .isupper()

This method return True if string contains upper case alphabets only. There must be at least one character in String.

Example

>>> name=’LOVEJOT’

>>> name.isupper()
True

>>> name1=’Lovejot’

>>> name1.isupper()
False

#Only first character is Capital



8. String .isspace()

This method return True if string contains space only. There must be at least one character in String.

Example

>>> name=’ ‘

>>> name.isspace()
True

>>> name1=” “

>>> name1.isspace()
True

9. String .lower()

This method converts string value into lowercase. There must be at least one character in String.

Example

>>> name=’LOVEJOT ‘

>>> name.lower()
‘lovejot’

>>> name1=”LOVEJOT2019“

>>> name1.lower()
‘lovejot2019’



 10. String .upper()

This method converts string value into capital letters. There must be at least one character in String.

Example

>>> name=’lovejot ‘

>>> name.upper()
‘LOVEJOT’

>>> name1=”lovejot2019“

>>> name1.upper()
‘LOVEJOT2019’

 11. String .lstrip()

This method removes spaces or specified character from the beginning of a string. There must be at least one character in String.

Syntax:

String. lstrip  ([ Char])

Char refers to the character that needs to be removed from the beginning of a String. All consecutive occurrences of character will be removed.

Example

>>> name=’   lovejot   ‘;

>>> name.lstrip()
‘lovejot   ‘

#Spaces removed from the beginning

>>> name1=’**lovejot**’

>>> name1.lstrip(‘*’)
‘lovejot**’

#*s removed from the beginning.



12. String .rstrip()

This method removes spaces or specified character from the end of a string. There must be at least one character in String.

Syntax:

String. rstrip  ([ Char])

Char refers to the character that needs to be removed from the end of a String. All consecutive occurrences of character will be removed.

Example

>>> name=’   lovejot   ‘;

>>> name.rstrip()
‘  lovejot’

#Spaces removed from the end.

>>> name1=’**lovejot**’

>>> name1.rstrip(‘*’)
‘**lovejot’

#*s removed from the end.

Lesson tags: built in string manipulation functions in python, python 3 string methods, string methods in python with examples
Back to: Python Programming Tutorial