Strings

Strings

'This is some text, or a string'
"This is also text..."

Strings are a list of characters, what we call text.

In Python strings must be enclosed in quotation marks, either 'text' or "more text".

Python does not mind which you use, but it is important to know where you need both.

Strings

#Error due to the apostrophe
'This is Joe's computer.'

Sometimes you need to use both forms of quotation marks in a string.

#Use double quotations in this case
"This is Joe's computer."
#When you need to include quotations
"He said, 'My computer is broken.'"
#Python will also work this way round
'He said, "My computer is broken."'

Apostrophes:

Quotations:

Multi-line Text

print('''A Person
21 Some Road
Big Town
County
PO51 2CD''')

There are times when it is useful to display several lines of text...

This is done by enclosing the text in three quotation marks:

Escaping Characters

# Display an apostrophe
print('David\'s computer')

" ' / are all characters that have meaning in Python and sometimes you want to use them in your strings.

This is done by escaping them with the backslash (\) symbol:

# Display a quotation
print("\"To be or not to be...\"")
# Display a backslash
print('When you just need to show a backslash \\')

Numbers and Strings

# Convert a string to an integer
string = input("Enter a value:")
num = int(string)

Input is always a string, so if you want to do calculations on user input you must convert the string to a number.

# Convert a string to an decimal
string = input("Enter a value:")
num = float(string)
# Convert a number to a string
string = str(number)

You may also need to convert a number to a string:

Length of Strings

sentence = 'Some words'
length_sentence = len(sentence)
len(word)

To find the length of a string...

len(sentence) will hold the value 10, because a space is a character.

Changing Case

aString.upper()
>> WELCOME TO COMPUTING
aString = "welcome TO computing"
aString.lower()
aString.capitalize()
aString.title()
>> welcome to computing
>> Welcome to computing
>> Welcome To Computing

Note: capitalize (american spelling)

Joining Strings (concatenation)

sentence = wordA + wordB + wordC
>> welcometocomputing
wordA = "welcome"
wordB = "to"
wordC = "computing"
sentence = wordA + " " + wordB + " " + wordC
>> welcome to computing
sentence = f"{wordA} {wordB} {wordC}"
>> welcome to computing

Removing Characters

aString.strip(' ')
#Remove spaces from a string
aString = 'Welcome to Computing'
aString.strip(' ')
>> WelcometoComputing
#Remove C from a string
aString = 'Welcome to Computing'
aString.strip('C')
>> Welcome to omputing

Getting Slices from a String

aString[start:end]
#Show only Comp
aString = 'Welcome to Computing'
aString[11:15]
>> Comp
#Show from the fist c
aString = 'Welcome to Computing'
aString[3:]
>> come to Computing

Tasks I

  1. Ask the user to enter their first name and then display the length of their name.
  1. Ask the user to enter their first name and then ask them to enter their surname. Join them together with a space between and display the name and the length of the whole name.
  1. Ask the user to enter their first name and surname in lower case. Change the case to title case and join them together. Display the finished result.
  1. Ask the user to type in the first line of a nursery rhyme and display the length of the string. Ask for a starting number and an ending number and then display just that section of text (remember python starts counting from 0 and not 1).

Tasks II

  1. Ask the user to type in any word and display it in an uppercase.
  1. Ask the user to enter their first name. If the length of their first name is under 5 characters, ask them to enter the surname and join them together (without a space) and display the name in uppercase. If the length of the first name is 5 or more characters, display their first name in lowercase.
  1. Pig Latin takes the first consonant of a word, moves it to the end of the word and adds on an "ay". If a word begins with a vowel you just add "way" to the end. For example, pig becomes igpay, banana becomes ananabay,  and aardvark becomes aardvarkway. Create a program that will ask the user to enter a word and change it into Pig Latin. Make sure the word is displayed in lower case.

C Strings

By David James

C Strings

Python - Strings

  • 566