ITS320 chp 7 string slicing

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

comparing string

String objects may be compared using relational operators (<, <=, >, >=), equality operators (==, !=), membership operators (in, not in), and identity operators (is, is not). The following provides examples, given my_str is 'Hello', student_name is 'Kay, Jo', and teacher_name is 'Kay, Amy':

inferred positional

empty {} assumes ordering of replacement fields is {0}, {1}, {2} 'The {} in the {}'.format('cat', 'hat') The cat in the hat

split function example

'Martin Luther King Jr.'.split(). The split method is applied to the string literal "Martin Luther King Jr." using any whitespace character as the default separator. The result of the method is the list of tokens ['Martin', 'Luther', 'King', 'Jr.'].

string slicing copying

3) Write a statement that assigns str2 with a copy of str1. str2 = str1[:]

named positional

a name matching a keyword argument 'The {animal} in the {headwear}'.format(animal='cat', headwear='hat') The cat in the hat

conversion specifier

a placeholder or a value in a string literal.

strings

a seqiemce type, having characters ordered by position from L to R

TOKEN

a sequence of characters that forms a part of a larger string

replacement field

a value from within the format() parentheses will be inserted into the string depending on the contents three ways positional, inferred, named

conversion flags

alter the output of conversion specifiers if the '0' conversion flag is included, numeric conversion types (%d, %f) add the leading 0s prescribed the minimum field width in places of spaces.

positional (replacement field)

an integer that describes the position of the value 'The {1} in the {0}'.format('hat','cat') The cat in the hat

two ways to fill replacement field

based on order of the values within the format() argument list. placing a number inside of a replacement field automatically treats the number as the position of desired value. 2nd way empty braces indicate that all replacement fields are positional and values are assigned in order L to R

split()

can be used to split up a strin into a list of tokens

percision

component of a conversion specifier indicates how many digits to the right of the decimal should be included. The precision must follow the minimum field width component in a conversion specifier, and starts with a period character: e.g., '%.1f' % 1.725 indicates a precision of 1, thus the resulting string would be '1.7'. If the specified precision is greater than the number of digits available, trailing 0s are appended: e.g., '%.5f' % 1.5 results in the string '1.50000'.

finding the position of where a character or substring is located in a strin

find(x)-- Returns the position of the first occurrence of item x in the string, else returns -1. x may be a string variable or string literal. Recall that in a string the first position is number 0, not 1. If my_str is 'Boo Hoo!': my_str.find('!') # Returns 7 my_str.find('Boo') # Returns 0 my_str.find('oo') # Returns 1 (first occurrence only) find(x, start)-- Same as above but begins the search at position start: my_str.find('oo', 2) # Returns 5 find(x, start, end)-- Same as above but stops the search at position end: my_str.find('oo', 2, 4) # Returns -1 (not found) rfind(x)-- Same as find but searches the string in reverse, returning the last occurrence in the string.

slice notation

has the form my_str[start:end] which creates a new string whose value mirrors the characters of my_str from positions start to end -1. Example: my_str = Boggle my_str[0:3] yields string 'Bog'

conversion type

indicate how to convert the value to a string. The d means integer, f is for float

conversion operator

inserts a value from the tuple on the right into conversion specifiers on the left.

separator

is a character or seuquence of characters that indocates where to split the string into tokens

checks string value in boolean value

isalnum()-- Returns True if all characters in the string are lowercase or uppercase letters, or the numbers 0-9. isdigit()-- Returns True if all characters are the numbers 0-9. islower()-- Returns True if all characters are lowercase letters. isupper()-- Return True if all cased characters are uppercase letters. isspace()-- Return True if all characters are whitespace. startswith(x)-- Return True if the string starts with x. endswith(x)--Return True if the string ends with x.

format() function

method can be used to format text. use placeholders within a string literal. Was supposed to replace %. placeholder is replacement field

join function example

my_str = '@'.join(['billgates', 'microsoft']) binds the name my_str to a new string object with the value 'billgates@microsoft'. The separator, '@', provides a join method that accepts a single list argument.

check string value example

name = input().strip().lower() reads in the user input, strips all whitespace, and changes all the characters to lowercase. Thus, user input of 'Bob', 'BOB ', or 'bob' would each result in name having just the value 'bob'.

join()

performs the inverse operation of split() by joining a list of strings together to create a single string

minimum field width

placed immediately before the conversion type describes minimum number of characters in the string. Example: The statement print('Student name (%5s)' % 'Bob') produces the output: Student name ( Bob)

finding and replacing strings`

replace(old, new)-- Returns a copy of the string with all occurrences of the substring old replaced by the string new. The old and new arguments may be string variables or string literals. replace(old, new, count)-- Same as above, except only replaces the first count occurrences of old.

mapping key

specified by indicating the key of the relevant value in the dict within parenthesis

stride

stride determines how much to increment the index after reading each element. For example, my_str[0:10:2] reads every other element between 0 and 10. The stride defaults to 1 if not specified.


Kaugnay na mga set ng pag-aaral

Pharmacology: Module 6- Lesson 2 Post Test

View Set

Coterminal Angles/Reference Angles/The Unit Circle

View Set

Maternity Test 2 Chapter 19 Application

View Set

UNIT 2-SECURITY REGULATION UNDER THE USA-2.1-2.3 (REVIEW QUESTIONS)

View Set