Data Types - String

String is a data type designed for text. It is created by surrounding text (or single characters) by either single quotation marks, or double quotation marks (there is no difference between those options).

Here is an example:

#String Assignment a = "tutorial" b = 'tutorial' print(a) print(type(a)) print(a == b)

Some operators can work on strings!
Python concatenates Strings using the + operator.
It is possible to multiply text using the * operator.

Here are some examples:

#Operators and String a = "tutorial" b = 'tutorial' c = "python" print(a + b) print(c + b) print(a * 5)

Access Characters

It is possible to access to different characters of a string by putting the appropriate index in square brackets next to the string string[index]. The index of the first character is 0. It is possible to use the negative index. The negative index of the last character is -1.

#Accessing Items quote = "We know what we are, but know not what we may be." print(quote) print("The first character of the quote is", quote[0]) print("The last character of the quote is", quote[-1])

Slicing - using colon it is possible to access a range of indexes.
The syntax:

  • string[stop]
  • string[start : stop]
  • string[start : stop : step]

#Slicing quote = "We know what we are, but know not what we may be." print(quote) #Using stop print(quote[:10]) #Using start, stop print(quote[0:10]) print(quote[0:-1]) #Using start, stop, step print(quote[0:10:3]) #This way we can access the entire string: print(quote[:])

Len

The len() function returns the length of a string.

#len() quote = "We know what we are, but know not what we may be." print(len(quote)) print(len(quote) * 2) print(len(quote[0:5]))

Split

The split() method gets two inputs - a string A and a substring B .
The method returns a list of substrings separated by substring B (watch example).

#split quote = "We know what we are, but know not what we may be." print(quote) print(quote.split("know")) print(quote.split(","))

Upper and Lower

Upper – returns a copy of the sting converted to uppercase.
Lower - returns a copy of the sting converted to lowercase.

#upper and lower quote = "We know what we are, but know not what we may be." print(quote.upper()) print(quote.lower())

Index

The index method returns the lowest index in a string where a substring is found (watch example).

#Index quote = "We know what we are, but know not what we may be." print(quote.index('know')) print(quote.index('we'))

Exercise 1

Try to slice the quote - drop the first and last words

#Exercise quote = "We know what we are, but know not what we may be." #Write your code here:
Find the index of the first and last words. Remember to use square brackets str[].

Exercise 2

  1. Find the middle character of the quote using the len() function
  2. Slice the first 10 letters of the quote, and print them in uppercase.

quote = "We know what we are, but know not what we may be." #Find the middle character here: #Slice here: print(quote[3:-4])

Exercise 3

  1. Split the qoute using a substring you choose.
  2. Split the quote using the character that is located on the 3rd index.
  3. Use the index() method to find the index of the word "what".
    Than slice the quote so that it begins from the word "what"

quote = "We know what we are, but know not what we may be." #1. Split the qoute using a substring you choose: #2. Split the quote using the character that is located on the 3rd index: #3.