What is a String?

A string is a sequence of characters and can be assigned by using quotes, either ‘Word’ or “Words” will work.

str1 = "Hello"
str2 = 'World'

 

With strings, the “+” operator means to concatenate. Concatenation is to combine.

str3 = str1 + str2
print(str3)
HelloWorld

 

Notice how the strings were concatenated. Since there was no space at the end of the first string, or at the beginning of the second string, the two strings were joined as is. If a space between them is desired, then you have to add a space.

new_str3 = str1 + ' ' + str2 
print(new_str3)
Hello World

 

If a string contains a number inside of it, it is still considered a string.

str4 = "I ate 8 tacos on taco Tuesday"

 

Strings can be indexed or sliced. If you don’t know what this means, refer to our Iteration and Looping page.