String

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

Given the list greeting = ["Hello", "my", "name", "is", "Earl"] what code would produce a string that contains: "Hello_my_name_is_Earl"

"_".join(greeting)

What output is returned from the following code: >>>"smooth".find('oo') ?

'2' You can also search for larger strings, and .find() will return the index value of the first character of that string.

Command to find character in string and return it's index in the string?

.find() >>> 'smooth'.find('t') '4' You can also search for larger strings, and .find() will return the index value of the first character of that string.

Command for including variables in a string?

.format() poet = 'old' title = 'boring' print('The poem \"{}\" is written by {}.'.format(title, poet)) You an also include keywords? print('The poem \"{}\" is written by {}.'.format(title=title, poet=poet))

Command to replace characters in a string?

.replace() STRING_NAME.replace(being_replaced, new_character)

Command for cleaning strings

.strip() Character specified between() will be stripped from data. If no value is specified all whitespace from beginning & end of string

Two ways to iterate through a string looking for a specific character?

1. Iterate through every character and use a conditional. 2. Use the type 'in' (letter in word) >>> "e" in "blueberry" True

Command to split each word in a sentence (on 'a') to a comma separated list for the string: greatest_guitarist = santana

>>> greatest_guitarist.split('a') ['s', 'nt', 'n', ' '] **Notice how split on a removes a in new list

Each character in a string is what?

A list foo = "bar" print(foo[1]) #b

Command to make string lowercase?

STRING.lower()

Command to make string a title?

STRING.title()

Command to make string uppercase?

STRING.upper()

Command to combine strings together (stored within a single list) with a common delimiter?

Syntax 'delimiter'.join(string_variable) words = ["Black", "reapers"] new_words = " ".join(words) "Black reapers"

Command to split each word in a sentence (with spaces) to a comma separated list?

line_one = "The sky has given over" line_one_words = line_one.split() ['The', 'sky', 'has', 'given', 'over']

Fix string with escape characters (escape the ") theycallme"crazy"91

password = "theycallme\"crazy\"91"


Set pelajaran terkait

Native American Studies Final Review

View Set

NUR3010-CHAPTER 1- PrepU Quizzes

View Set

EDAPT week 1 - Introduction to Nutrition,Health & Wellness

View Set

ECON101 Exam 2: Problem Sets and Quizzes

View Set

BME Financial Analysis Unit Exam

View Set

DAILY ROUTINE, FREE TIME, HOBBIES / QUESTIONS AND ANSWERS

View Set