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.


eat = input(”food”)

--Originally published at Just A Turtle Coding.

Last night, Owner decided not to feed me since he was working on Ken’s topics. I saw him using the input function and all I could think of was food… I was starving. So when he went downstairs to get some food, I crawled to the keyboard and wrote: 

When he returned, he saw a program. He ran it and I ate. Everyone’s happy now. :D
What input does is it stops the program until the user running it inputs what’s asked.

Python’s “Colors”

--Originally published at Just A Turtle Coding.

As everyone knows, life has its colors. Python does as well, everything you write is something, and those somethings have names. Owner taught me that while he gave me a “somethingberry”…

image

But he also tells me to bite small so I don’t choke myself… So lets bite small.

Variables

Variables are like the “berries” of Python. You can have STRAWberries, you can have RASPberries, you can have BLUEberries but they are all a part of the berries family.

Variables include numbers. Numbers are what the name says they are, numbers, but numbers have types also:

  • Int: a simple whole number. (Integer)
  • Long: a REAAAAAALLY long number.
  • Float: A number with decimal point.
  • Complex: A complex number. It’s usually written like: (9-4j) where 9 is the real part and the number followed by the letter “j” is the imaginary part.

You can also have another “berry”: Strings.

Strings are just characters (text or numbers) enclosed by a quotation marks. You can do all sorts of things with strings like write them lots of times by putting a * or add things to them. This image shows mostly everything <3