Busy. Please wait.

show password
Forgot Password?

Don't have an account?  Sign up 

Username is available taken
show password


Make sure to remember your password. If you forget it there is no way for StudyStack to send you a reset link. You would need to create a new account.
We do not share your email address with others. It is only used to allow you to reset your password. For details read our Privacy Policy and Terms of Service.

Already a StudyStack user? Log In

Reset Password
Enter the associated with your account, and we'll email you a link to reset your password.

Remove Ads
Don't know
remaining cards
To flip the current card, click it or press the Spacebar key.  To move the current card to one of the three colored boxes, click on the box.  You may also press the UP ARROW key to move the card to the "Know" box, the DOWN ARROW key to move the card to the "Don't know" box, or the RIGHT ARROW key to move the card to the Remaining box.  You may also click on the card displayed in any of the three boxes to bring that card back to the center.

Pass complete!

"Know" box contains:
Time elapsed:
restart all cards

Embed Code - If you would like this activity on your web page, copy the script below and paste it into your web page.

  Normal Size     Small Size show me how


Python Strings

string definition denoted str used to represent and manipulate text data or, a sequence of characters, including blanks, punctuation, and various symbols. A string value is represented as a sequence of characters that is enclosed within "quotes".
string operators ==, !=, <, >, and so on returns True/False
addition makes new string when applied to two strings, evaluates to a new string that is the concatenation (i.e., the joining) of the two strings
multiply 'string' * 'string' = error 'string' * 2 = 'stringstring' 'string ' * 2 = 'string string'
length of string len(s)
Character of string s at index i s[i]
Concatenation of n copies of s s*n n*s
Concatenation of string s and string t s + t
False if string x is a substring of string s, and true otherwise x not in s
True if string x is a substring of string s, and false otherwise x in s
indexing operator [] takes a nonnegative index i and returns a string consisting of the single character at index i Negative indexes access the characters from the back (right side) of the string. For example, the last character can be retrieved using negative index -1.
index the character's offset (i.e., position in the string) with respect to the first character. The first character has index 0, the second has index 1 (because it is one away from the first character), the third character has index 2, and so on.
Can you change a string? NO. Strings are immutable. Can make new string only.
forgetting quote delimiters no quotes, text will be treated as a name. >>> hello Traceback (most recent call last): File “<pyshell#35>”, line 1, in <module> hello NameError: name ‘hello’ is not defined Error message reports that name hello is not defined.
What to do if string text contains quotes If the text contains a single quote, we can use double quote delimiters, vice versa. If the text contains both quote types, then the escape sequence \’ or \” is used to indicate that a quote is not the string delimiter but is part of the string value.
How do I not have the escape sequence show up? the print() function will interpret any escape sequence in the string and omit the string delimiters
escape sequence in a string is a sequence of characters starting with a \ that defines a special character and that is interpreted by function print()
multi-line strings use triple quotes ''' encode the new line characters explicitly
new line character The escape sequence \n stands in for a new line character, also When it appears in a string argument of the print() function, the new line escape sequence \n starts a new line
slice The expression s[0:2] evaluates to the slice of string s starting at index 0 and ending before index 2.
slice containing first or last character first s[:3] last s[2:]
string method s.find() When it is invoked on string s with one string input argument target, it checks whether target is a substring of s. it returns the index (of the first character) of the first occurrence of string target; otherwise, it returns -1.
string method s.count() The method count(), when called by string s with string input argument target, returns the number of times target appears as a substring of s.
string method s.replace(a, b) The function replace(), when invoked on string s, takes two string inputs, old and new, and outputs a copy of string s with every occurrence of substring old replaced by string new.
replace (a, b) and assignment statement the string is not changed (immutable) by replace. to use the replace changes must assign string to something ergo, make new string
string method s.capitalize () Method capitalize(), when called by string s, makes the first character of s uppercase;
string method s.upper() method upper() makes all the characters uppercase.
string method s.split() the method split() uses the blank spaces in string ‘this is the text’ to create word substrings that are put into a list and returned.
split() with delimiter The delimiter string is used in place of the blank space to break up the string. For example, to break up the string >>> x = ‘2;3;5;7;11;13’ into a list of number, you would use ‘;’ as the delimiter: >>> x.split(‘;’) [‘2’, ‘3’, ‘5’, ‘7’, ‘11’, ‘13’]
string method translate() mapping is constructed using a table that is called not by the string class str itself: >>> table = str.maketrans(‘abcdef’, ‘uvwxyz’) >>>‘fad’.translate(table) ‘zux’ ch6
string method s.lower() A copy of string s converted to lowercase
string method s.strip() A copy of string s with leading and trailing blank spaces removed
print() a function used to print values onto the screen. input is an object output it prints a STRING REPRESENTATION of the object's value.
print() default separater takes an arbitrary number of input objects, not necessarily of the same type. The values of the objects will be printed in the same line, and blank spaces (i.e., characters ‘ ’) will be inserted between them as a default separater.
print() defined separater The print() function takes an optional separation argument sep: >>> print(n, r, name, sep=‘;’) 5;1.66666666667;Ida The argument sep=‘;’ specifies that semicolons should be inserted to separate the printed values.
how separater works In general, when the argument sep=<some string> is added to the arguments of the print() function, the string <some string> will be inserted between the values. common used for (;), (\n)
end=<some string> end=<some string> string <some string> is printed after all the arguments have been printed, eliminating the implied \n >> for name in [‘Joe’, ‘Sam’, ‘Tim’, ‘Ann’]: print(name, end=‘! ’) Joe! Sam! Tim! Ann!
printing using format () >>> hour = 11 >>> minute = 45 >>> second = 33 >>> ‘{0}:{1}:{2}’.format(hour, minute, second) ‘11:45:33’ characters outside {} are printed as is. {0}, {1}, and {2} are placeholders where the objects will be printed no {number}, defaults L2R
lining up data >>> ‘{0:3},{1:5}’.format(12, 354) ‘ 12, 354’ The 0 refers to the first argument of the format() function (12), In this case, 3 indicates that the width of the placeholder should be 3. right justified
precision >>> ‘{:8.4}’.format(1000 / 3) ‘ 333.3’ Compare this with the unformatted output: >>> 1000 / 3 333.3333333333333
type format binary Outputs the number in binary >>> n = 10 >>> ‘{:b}’.format(n) ‘1010’
type format unicode >>> n = 10 >>> ‘{:c}’.format(n) ‘\n’ c Outputs the Unicode character corresponding to the integer value
type format decimal >>> n = 10 >>> ‘{:d}’.format(n) ‘10’ d Outputs the number in decimal notation (default)
type format base 16 lowercase >>> n = 10 >>> ‘{:x}’.format(n) ‘a’ x Outputs the number in base 16, using lowercase letters for the digits above 9
type format base 16 uppercase >>> n = 10 >>> ‘{:X}’.format(n) ‘A’ X Outputs the number in base 16, using uppercase letters for the digits above 9
type format base 8 o Outputs the number in base 8
Created by: kbaldwin