Python Strings

Strings in Python

What is strings?

Python strings are data type contains any character surrounded by quotes. Following are types of string :

  • Single-quoted strings : Strings enclosed with single quotes.
  • Double-quoted strings : Strings enclosed with double quotes.
  • Triple-quoted strings : Strings enclosed with triple quotes.





Strings Slicing

Python string slicing allows to slice characters from the strings. Indexing always start from the zero(0) means string's first character indexed at zero(0) shown below.  Following ways are used in string slicing :
  • Single Character Indexing : Slice a single character from the string. By giving an index from the string.


  • Multiple Character Indexing : Slice multiple character from the string. By giving the start index and last index, separated by colon.


  • Negative Indexing : Slice characters with the help of negative indexing. By giving the negative start index and last index, separated by the colon.


  • Skip Indexing : Slicing the string by skip the characters. By giving the start index, last index and skip index, separated by the colon.




String Methods

Python string methods are used to manipulate the strings. Following are useful built-in methods :

  •  len(string)  : Used to find the length of the string.
  •  string.capitalize()  : Used to convert the first character of the string into upper case.
  •  string.count()  : Used to count the occurrence of a specific character in the string.
  •  string.endswith()  : Used to find whether the string ends with the given character or not.
  •  string.find()  : Used to find the position of the given character in the string.
  •  string.replace()  : Used to replace specified character from the given value.

  •  string.upper()  : Used to convert whole character of the string into upper case.





Tell me one more method of string in comment section.

Comments