Python 2.7 – Fundamentals – Collections – Strings

A String is a collection of characters. What are characters? In simple terms, all the letters, numbers and symbols that you find on a keyboard are each called a character. So A 9 & are examples of three characters.  A space is also a character. When you group characters together, it is called a String. Strings in Python are written surrounded by single quotes or double quotes.

Eg. ‘cat’ or “cat”

If you try to use strings without the surrounding quotes, you will get an error. You can also have an empty string – a string without any characters. Eg. “” or ” . The length of a string is the number of characters in the string. Strings are immutable i.e once you declare a string variable you cannot change its value without creating another string.

Operators

The following operators are available for strings:

  1. [] – index operator.
  2. [:]  – slicing
  3.  + adding

We will look at the above in more detail.

[] – index operator

As mentioned before, strings are collections of characters. So to access individual characters in a string we use the index operator. The lowest index value is zero which represents the first character and the highest index value is the length of the string minus one.

 

 

 

 

 

 

 

 

As you can see above print name[13] gives an error since the length of the string is 12. For an empty string blank = ” the length of the string is zero, so print blank[0] gives an error.

[:] slicing

The index operator lets you extract one character from the string. The slicing operator lets us extract multiple characters from a string. The operator takes two numbers [x:y] where x is the starting index and y is the ending index. Note that the ending index is not inclusive eg. if you try print name[1:3] it will extract character at index 1 and then stop at index 2.

The slicing operator has various permutations and combinations as shown below:

  • [x:y] – extract from x and stop before y
  • [x:] – extract from x till the rest of the string
  • [:y] – extract from the first character till y
  • [-x:] – extract x characters from the end
  • [:-y] – extract from the first character and stop before y
  • [:] – no x and y . Extract the whole string

 

 

 

 

 

 

 

+ adding

Adding two strings together gives a new third string. Examples are shown below:

 

 

 

 

 

 

Methods

Methods are functions which are available for string variables. The following methods are available:

  • lower() – convert string to lowercase
  • upper() – convert string to uppercase
  • strip()  – remove any leading or trailing spaces in the string
  • isalpha() – check if all characters in string are alphabets
  • isdigit() – check if all characters in string are digits
  • isspace() – check if all characters in string are spaces
  • startswith(x) – check if string starts with the string specified by x
  • endswith(x) – check if string ends with the string specified by x
  • find(x) – checks if string contains string specified by x and returns the index if found else returns -1
  • replace(x, y) – replaces all occurrences of string x with string y
  • split(x) – splits a string into a list using the delimiter specified by string x
  • join(x) – reverse of split() . Converts separate strings in a list into a string using this string as a delimiter

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The startswith() and endswith() methods are case-sensitive as are the other methods. The split() and join() methods deal with lists which we will read about in the next posts.

There are other string methods, but the ones mentioned above are the basic methods which cover all the possible operations on strings.

In the next post we look at lists .

1 Trackback / Pingback

  1. Python 2.7 – Fundamentals – Collections – Truelogic Blog

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.