Python Programming

Tutorial No. 8 : Working with Strings in Python

Working with Strings in Python

1. capitalize() : capitalize() method converts first character of a string to uppercase letter.The capitalize() function returns a string with the first letter capitalized and keep other characters lowercased. It doesn't modify the original string.

a = "nuv"
print(a)
b = a.capitalize()
print(b)
Output :
nuv
Nuv

2. center() : center() method returns a string which is filled with the specified character.

a = "nUV"
print(a)
b = a.center(24)
print(b)
Output :
nuv
                    nuv

3. count() : count() method returns the number of occurrences of a substring in the given string.

a = "My name is is"
b = a.count("is")
print(b)
Output :
2

4. casefold() : casefold() method converts all teh characters of given string into lowercase.

a = "Hello NUV"
b = a.casefold()
print(b)
Output :
hello nuv

5. find() : find() method finds the first occurrence of the specified value.The find() method returns -1 if the value is not found.

a = "This is very good concept"
b = a.find("is")
print(b)
Output :
2

6. index() : index() method(same as find() method) finds the first occurrence of the specified value.The index() method raises error if the value is not found.

a = "This is very good concept"
b = a.index("is")
print(b)
Output :
2
Now in above example change the is with now and check the result

7. isalnum() : isalnum() method returns True if all characters in the string are alphanumeric (either numbers or alphabets). returns False otherwise.

a = "Thisis007"
b = a.isalnum()
print(b)
Output :
True
Now in above example give space in between This and is and again check

8. isalpha() : isalpha() method returns True if all characters in the string are alphabets. returns False otherwise.

a = "Thisis"
b = a.isalpha()
print(b)
Output :
True
Now in above example give space in between This and is and again check

9. isdigit() : isdigit() method returns True if all characters in the string are digits. returns False otherwise.

a = "007"
b = a.isdigit()
print(b)
Output :
True
Now in above example,change string with This 007 and check output again

10. isupper() : isupper() method returns True if all characters in the string are in uppercase. returns False otherwise.

a = "HI HELLO"
b = a.isupper()
print(b)
Output :
True

11. islower() : islower() method returns True if all characters in the string are in lowercase. returns False otherwise.

a = "hi hello"
b = a.islower()
print(b)
Output :
True

12. lower() : lower() method converts all the characters of a given string into lower case.

a = "HiHello"
b = a.lower()
print(b)
Output :
hihello

13. upper() : upper() method converts all the characters of a given string into upper case.

a = "hihello"
b = a.upper()
print(b)
Output :
HIHELLO

14. isprintable() : isprintable() checks whether all the characters of the given string is printable or not.

a = "hihello"
b = a.isprintable()
print(b)
Output :
True
Change the the code of line 1 as : a = "\nhihello" and then check