Creation and use of strings

--Originally published at Quirino´s Projects

string is a data type which contains a set of concatenated characters, is one of the simplest data type in python

A string must be between “” or ”, there can be empty strings like the ones mentioned before, or strings that contain just a space like ” ” we can use strings to store names, addresses, colors, well everything that can be written, a word or a phrase, there is no limit.

name = “Juan”

address = “Milan  #### Col. Providencia ”

numbersFromOneToThree = “one\ntwo\nthree”

If we would print the three variables we would get

“Juan”

“Milan  #### Col. Providencia”

“one

two

three

As you can see \n prints a new line, this happens because \ is a special character that can make the string do different things with the char next to it, using \n makes a new line \” or \’ makes you able to print the ” ‘ without ending or starting a new string

String operations, you can also perform sums with strings too, adding a character to a string or a string to a string will concatenate the given string to the string we are operating with

We can also pass strings to a for, thus we can operate each char individually

newString = “”

for i in “Juan”:

newString += (i.upper() + “_”)

print (newString) #J_U_A_N

We can multiply strings to get a bigger one containing the string itself

print (“Yiii” * 3) # prints “YiiiYiiiYiii”

For more info (in spanish) check this great tutorial http://librosweb.es/libro/python/capitulo_6.html