Strings#


Strings ~ Lists#

  • In Python, Strings are very similar to Lists

  • Lists can contain any type of data i.e. integers, characters, booleans, strings etc.

  • Strings can only hold a set of characters

Similarities between Strings and Lists#

  • Individual characters can be accessed using the same square brackets syntax variable_name[index]

  • You can call the built-in method len on strings, just like lists

  • Positive Indicies start from 0 to length - 1

  • Negative indicies go from -1 to -length

Slicing for Strings works the same as in lists#

Differences between Strings and Lists#

  • Strings can only hold characters, although characters themselves can be anything e.g. “13”, “[1, 2, 3]”, “True” etc.

    • Lists can can contain any type of data

  • Strings are immutable, unlike lists

    • All operations return a new string

  • Strings are passed as value to functions

    • Not by reference

    • Any changes made to arguments inside a function are not reflected at the caller

Comparison with Lists#

  • Directly initialize with values

  • Using single or double quotes

  • If you can use single quote to delimit your string value, you can use double quotes as a character within value and vice versa

  • There is no built-in append method for strings

  • Instead, you can concatenate two (or more) strings to each other using + operator

  • Removing characters

    • Use slicing, no pop or remove

    • Or .replace(characters, '')

Decoding#

  • Encoding is the process of converting information from a source into symbols for communication or storage.

  • Decoding is the reverse process of encoding: converting code symbols back into a form that the recipient understands.




msg = [7, 4, 17, 4, 18, " ", 9, 14, 7, 13, 13, 24]
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
msg = "semitemos dam elttil a og lla ew"
msg = "PRESIDENT'S EMBARGO RULING SHOULD HAVE IMMEDIATE NOTICE. GRAVE SITUATION AFFECTING\
    INTERNATIONAL LAW. STATEMENT FORESHADOWS RUIN OF MANY NEUTRALS. YELLOW JOURNALS UNIFYING \
    NATIONAL EXCITEMENT IMMENSELY."