String literals can be enclosed in either two single or two double quotes. The reason for this is so you can use one type inside the other to print out the inner quote. Even though you could use an escape sequence to do the same, this option is still available.

Concatenationconcatenate = the act of putting two objects end to endYou can concatenate adjacent string literals. You can put a "+" between them, or wrap them in parenthesis allowing to span multiple lines.

A multiline text data that can be of as many lines as you need. It can be either double quotes """ """ or single quotes ''' '''. Single or double quotes inside the triple quote do not have to be escaped, but can be. It is useful for error messages. HTML code as strings, documentation, disable partial code temporarily.

>>> triple = '''This is... some gibberish that... I have came up with.'''>>> triple'This is\nsome gibberish that\nI have came up with.'

Escape Sequences

Back slashes are used to allow special byte codes: Escape Sequences.The backslash "\" and the following character(s) are replaced by a single character which has a binary value specified by the character(s).

This string gives this result because of the escape sequences in it. The 2 is on a newline because of \n, the 3 is tabbed because of the \t, and the backslash also on a newline and is shown because we escaped the backslash itself with \\. The built-in function len() returns the length of the string which is showing 7 and not 11 because escape sequences are one character and thus is showing 7 bytes.

Raw Strings

An r preceding the string will make it a raw string. This will ignore the escape sequences inside the string. Otherwise the code below would have a \n and a \t in it.

One of the most used features of Python is indexing and slicing. These are the acts of accessing the characters in the string by position, assuming you are doing it to a string. This can also be done to a list of elements.

You could think of it like this: s[START:END]. Give me the string omitting before and up to, not including START index ... and omit END and after it. If no START, then give me all the way from beginning, if no END give me all the way to the end. Slicing will get easier the more you use it.

Not as if you are already confused, but there is also a third index in the slice s[START:END:STEP]. This STEP index will allow you to get the sliced string by skipping STEP. For example, a step of 2 will give you every other index based on your values of START and END.

Were you expecting 2? Were you expecting '2'? Were you expecting '11'? Python doesn't know which one you want...a string...or an integer. To bypass, you need to either convert the '1' to an int 1, or convert the int 1 to a string '1'. Converting both to strings, however will get you the string '11' Converting both to integers will get you 2. The built in functions str() will convert to a string and the int() will convert to an integer.

Character Code Conversionsord() and chr()Every character on a computer has an ASCII integer code. The built in functions ord() will convert the single character to its ASCII integer, while chr() will do the opposite and convert the ASCII integer to it's single character.

str.capitalize(), str.title(), str.lower(), str.upper(), str.swapcase()All of these methods effect the outcome of the capitalization of words inside of a string. The method capitalize() will capitalize the first character of the first word. The method title() will capitalize every first character of every word in the string. The method lower() will make all characters lowercase in the string. The method upper() will make all characters uppercase in the string. The method swapcase() will swap uppercase characters for lowercase characters and vice versa in the string.

>>> s = 'the great beyond of the stars'>>> s.capitalize()'The great beyond of the stars'>>> s.title()'The Great Beyond Of The Stars'>>> s2 = s.title()>>> s2'The Great Beyond Of The Stars'>>> s2.swapcase()'tHE gREAT bEYOND oF tHE sTARS'>>> s.upper()'THE GREAT BEYOND OF THE STARS'

str.split()The method split() will chop the string into substrings into a list.

There are numerous string methods available. Check >>>help(str) to view them in the python interpreter. There is also a very common str method that I gave its own section for due to it's complexity: the str.format(). This section can be found in string expressions and formatting.

Format Expression and Format MethodThere are two ways to format strings. The first is the Expression (which it's character is known for as %) and the second is the newer technique: the format method str.format().

Format ExpressionsThe % operator when applied to strings provides a way to format a string according to it's format definition. The way it is applied is like so:

When you plug in more than two values into the string they need to be within parenthesis. The %i gets replaced by the integer 2 and the %s gets replaced by string 'ways'. If you do not input the correct definition for the corresponding value you will get an error. The below code shows an example of attempting to input two integers where one of them is a string.

As you can see. The type of data it is, does not matter. Here, the {} indicate the definition of whatever is in the order of the format()'s arguments. By default empty curly brackets {} plug format()'s arguments into the string at their position they are in format(). You can also change this order of postion.

Within the formatspec a subcategory of: [[fill]align][sign][#][0][width][.precision][typecode] gives the specifications of how it should be presented. Align can be <, >, =, or ^, for the left alignment, right alignment, padding after a sign character, or centered alignment. The formatspec also contains a nested {} format strings with field name only.