Creation and use of strings

--Originally published at My B10g

 

Strings in python are like lists where you can get each letter using its position making them easier to manage than in other languages.

although strings are letters you can also do operation with them, like;

+,*,[:],in, not in

you can add too strings using the + plus, or write something several times using * times, also you can take a substring from the strings using [:] and numbers to determine the length of the substring, or use “in” to check if a substring is inside the string in case that it does it returns True and in case of the “not in” its the oposite, it return True in case its not a substring.

example:

Captura de pantalla 2016-10-26 a las 16.24.22.png

apart from operation strings also have build-in methods which we can use to measure, search, compare, find if it’s a digit,etc.

String Methods

  • count (substring, begin, end): Counts and returns how many times the substring given was found in the String within the range given
  • endswith (suffix, begin, end): Returns true if a string ends with the suffix given
  • find (substring, begin, end): Determines if the substring is within the string and, if found, it returns its position. The method will return the first substring it finds, if not fount, it will return -1.
  • isdigit(): Returns true if the string contains only digits
  • len(string) returns the length of the string

 

Note: in a string you can’t print “\n” or “\t” because it adds an enter an a tab to the text respectively.