2018-02-14

posted Feb 14, 2018, 5:20 AM by Konstantinovich Samuel   [ updated Feb 14, 2018, 6:24 AM ]

This is a visualizer, so you can see what your code is actually doing:
http://pythontutor.com/visualize.html

,d88b.d88b,
88888888888
`Y8888888Y'
  `Y888Y'
    `Y'


Here is a large list of string methods:

Strings in Python are immutable. (They cannot be changed)
In order to 'change' them we re-assign them. 
Alternatively we make a new string that is different and use that modified copy.

Concatenation does not change the string:
x = "pan"
y = "duh"
print x + y #concatenation makes a new string for the result
            #it does not modify the operands (prints a modified copy)
print x     #still "pan"
print y     #still "duh"

String methods (see below) do not change the string:

print x.upper()  #Shows: PAN  (prints a modified copy)
print x          #shows: pan

Only assignment changes a string variable:
print y       #still "duh"
y = x + y     #change y to "pan"+"duh" (reassigns y to the modified copy)
print y       #shows: panduh
print x       #shows: pan



Here is a subset of string methods that exist:

Counting/Finding

str.count(sub[, start[, end]])
Return the number of non-overlapping occurrences of substring sub in the range [start, end]. 
Optional arguments start and end are interpreted as in slice notation.

str.find(sub[, start[, end]])
Return the lowest index in the string where substring sub is found within the slice s[start:end]. 
Optional arguments start and end are interpreted as in slice notation. Return -1 if sub is not found.

    str.rfind(sub[, start[, end]])    
    Return the highest index in the string where substring sub is found, such that sub is contained within s[start:end]. 
    Optional arguments start and end are interpreted as in slice notation. Return -1 on failure.

str.index(sub[, start[, end]])
Like find(), but raise ValueError when the substring is not found.

    str.rindex(sub[, start[, end]])
    Like rfind() but raises ValueError when the substring sub is not found.



Checking the string:

str.isalnum()
Return true if all characters in the string are alphanumeric and there is at least one character, false otherwise.

str.isalpha()
Return true if all characters in the string are alphabetic and there is at least one character, false otherwise.

str.isdigit()
Return true if all characters in the string are digits and there is at least one character, false otherwise.




Make Changes to string:
str.capitalize()
Return a copy of the string with its first character capitalized and the rest lowercased.

str.lower()
Return a copy of the string with all the cased characters [4] converted to lowercase.

str.upper()
Return a copy of the string with all the cased characters [4] converted to uppercase. Note that str.upper().isupper() might be False if s contains uncased characters or if the Unicode category of the resulting character(s) is not “Lu” (Letter, uppercase), but e.g. “Lt” (Letter, titlecase).

str.replace(old, new[, count])
Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

str.strip([chars])
Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped:

  str.lstrip([chars])
  Return a copy of the string with leading characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix; rather, all combinations of its values are stripped:

  str.rstrip([chars])
  Return a copy of the string with trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a suffix; rather, all combinations of its values are stripped:




Comments