CIS 156: Python Programming - Strings (Chapter 7)

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

ASCII table

It provides a quick lookup of ASCII values.

islower()

It returns True if all cased characters are lowercase letters.

isupper()

It returns True if all cased characters are uppercase letters.

isdigit()

It returns True if all characters are the numbers 0-9.

isspace()

It returns True if all characters are whitespace.

isalnum()

It returns True if all characters in the string are lowercase or uppercase letters, or the numbers 0-9.

endswith(x)

It returns True if the string ends with x.

startswith(x)

It returns True if the string starts with x.

title()

It returns a copy of the string as a title, with first letters of words capitalized.

lower()

It returns a copy of the string with all characters lowercased.

upper()

It returns a copy of the string with all characters uppercased.

replace(old, new)

It returns a copy of the string with all occurrences of the substring old replaced by the string new.

strip()

It returns a copy of the string with leading and trailing whitespace removed.

capitalize()

It returns a copy of the string with the first character capitalized and the rest lowercased.

find(x)

It returns the index of the first occurrence of item x in the string, else returns -1.

count(x)

It returns the number of times x occurs in the string.

split()

It splits a string into a list of tokens.

air_temperature = float(input()) print( f"{round(air_temperature, 1)}C")

Print air_temperature with 1 decimal point followed by C. - Sample output with input: 36.4158102 - 36.4C

find(x, start)

It is the same as find(x), but begins the search at index start.

find(x, start, end)

It is the same as find(x, start), but stops the search at index end - 1

string = input() normal = "" reverse = "" for i in range(len(string)): if string[i].isalpha(): normal += string[i].lower() reverse = string[i].lower() + reverse if normal == reverse: print(string + " is a palindrome") else: print(string + " is not a palindrome")

A palindrome is a word or a phrase that is the same when read both forward and backward. Examples are: "bob," "sees," or "never odd or even" (ignoring spaces). Write a program whose input is a word or phrase, and that outputs whether the input is a palindrome. - Ex: If the input is: bob - The output is: bob is a palindrome - Ex: If the input is: bobby - The output is: bobby is not a palindrome - Hint: Start by removing spaces. Then check if a string is equivalent to it's reverse.

phrase = input() result = '' for word in phrase: if word.isupper(): result += word print(result)

An acronym is a word formed from the initial letters of words in a set phrase. Write a program whose input is a phrase and whose output is an acronym of the input. If a word begins with a lower case letter, don't include that letter in the acronym. Assume there will be at least one upper case letter in the input. - Ex: If the input is: Institute of Electrical and Electronics Engineers - The output is: IEEE

user_tweet = input() decoded_tweet = user_tweet.replace("TTYL", "talk to you later") print(decoded_tweet)

Assign decoded_tweet with user_tweet, replacing any occurrence of 'TTYL' with 'talk to you later'. - Sample output with input: 'Gotta go. I will TTYL.' - Gotta go. I will talk to you later.

phone_number = input() number_segments = phone_number.split("-") area_code = number_segments[0] print('Area code:', area_code)

Assign number_segments with phone_number split by the hyphens. - Sample output with input: '977-555-3221' - Area code: 977

start_index = int(input()) end_index = int(input()) rhyme_lyric = 'The cow jumped over the moon.' sub_lyric = rhyme_lyric[start_index:end_index] print(sub_lyric)

Assign sub_lyric by slicing rhyme_lyric from start_index to end_index which are given as inputs. - Sample output with inputs: 4 7 - cow

user_tweet = input() if 'LOL' in user_tweet: print('LOL means laughing out loud.') else: print('No abbreviation.')

Complete the if-else statement to print 'LOL means laughing out loud' if user_tweet contains 'LOL'. - Sample output with input: 'I was LOL during the whole movie!' - LOL means laughing out loud.

Fill Character

It is used to pad a replacement field when the string being inserted is smaller than the field width.

user_string = input() output = user_string.isnumeric() if output == True: print("yes") else: print("no")

Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9. - Ex: If the input is: 1995 - The output is: yes - Ex: If the input is: 42,000 - Or any string with a non-integer character, the output is: no

my_str[start:end]

It creates a new string that a value contains the characters on which it indices from start to end - 1.

Slice Notation

It defines a starting point, the stopping point, and the step size

Field Width

It defines the minimum number of characters that must be inserted into the string.

Alignment Character

It determines how a value should be aligned within the width of the field.

Stride

It determines how much to increment the index after reading each element.

Precision

It indicates how many digits to the right of the decimal should be included in the output of floating types.

Separator

It is a character or sequence of characters that indicates where to split the string into tokens.

join()

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

Token

It is a substring that forms a part of a larger string.

Index

It is an integer matching to a specific position in a string's sequence of characters.

replace(old, new, count)

It is the same as above, except only replaces the first count occurrences of old.

rfind(x)

It is the same as find(x) but searches the string in reverse, returning the last occurrence in the string.

(==, !=)

They are equality operators.

(is, is not)

They are identity operators.

(in, not in)

They are membership operators.

(<, <=, >, >=)

They are the string objects that may be compared using relational operators.

user_input = input() def checkLetters(str): output = "" for char in str: if char.isalpha() == True: output += char return output print(checkLetters(user_input))

Write a program that removes all non-alpha characters from the given input. - Ex: If the input is: -Hello, 1 world$! - The output is: Helloworld

print(end='') word = input() char = word[0] print(str((word.count(char))-1))

Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase. - Ex: If the input is: n Monday - The output is: 1 - Ex: If the input is: z Today is Monday - The output is: 0 - Ex: If the input is: n It's a sunny day - The output is: 2 - Case matters. - Ex: If the input is: n Nobody - The output is: 0 - n is different than N.


Set pelajaran terkait

[Music Theory] chapter 07: triads

View Set

chapter 7 (middle childhood: body and mind)

View Set

Upper limb trapezius, levator scapulae, and rhomboid major + minor pg 300

View Set

T3: Lesson 2 - The Origins of Hinduism & Buddhism -- Text 2: The Caste System Shapes India

View Set

Chapter 58: Drugs Affecting Gastrointestinal Motility

View Set

Adobe Photoshop CC18 Practice Training Test

View Set