Sunday, May 30, 2021

Python - Strings

Strings are series of characters represented within double quotes.

Declaration my_string=""  creates an empty string.

String indexing:

my_string="Hello World"

positive indexing starts from 0 to len(my_string)-1

my_string[0]="H"

my_string[1]="e"

negative indexing starts from -1 for len(my_string) to -len(my_string)

my_string[-1]="d"

my_string[-2]="l"

 

String functions:

1.strip()  - removes leading and trailing spaces in a string.

   strip("#") - removes leading and trailing # in a string.

  • lstrip - removes space/character at the beginning.
  • rstrip - removes space/character at the end.

 

2.len() - returns the length of the string.

 

3.swapcase() - changes upper case letter to lower case and vise versa.

 

4.startswith() - If the string starts with the given character returns True. Else returns False.

 

5.endswith() - If the string ends with the given character returns True. Else returns False.

 

6.split() - splits string into two and stores them as a string.

7.rsplit() - splits the string when a specified character is met.

 8.splitlines() - splits the string when a new line is met.

 

9.index() - returns the index value of specified character.

 

10.rindex() - returns the index value of specified character.If there are multiple specified characters it returns the index value of specified character that occurred last.

 

11.find() - same as index()

 

12.rfind() - same as rindex()

 

Then what is the difference between find and index?!

 

13. replace() - replaces the string/character with new string.

 

14.capitalize() - capitalizes the 1st word of a string.

 

15.join() - used to join a string with character specified.

 

16.count() - returns the count of specified character.

 

17.isalnum() - returns true if string has only alphabets and numbers.

Else returns False.

 

18.isnumeric() -  returns True if the string has only numbers.

Else returns False.

 

19.isalpha() - returns true only if string has only alphabets.

 

20.isdigit() - returns true only if character is number.

 

21.isupper() - if the character is upper case returns True, else returns false. 

 

 

22.islower() - if the character is lower case returns True, else returns false. 

 

23.isspace() - if the character is lower case returns True, else returns false. 

 

24.isdecimal() - if the string is decimal returns True, else returns false. 

 

25.upper() - converts the character to upper case.


26.lower() - converts the character to lower case.

 

27. ljust() - aligns the string to left and fills the remaining width with spaces.


28. rjust() - aligns the string to right and fills the remaining width with spaces.

 

29.center() - aligns the string to center and fills the remaining width with spaces.

 

30.casefold() - converts the string to lower case.

  

No comments:

Post a Comment

Anaconda Installation

In this post we will discuss about Anaconda and its installation. Anaconda comes with number of applications(jupyter notebook included), dat...