String length and indexing
A common operation is to find the length, or the umber of characters, in a
string. The len() built-in function can be used to find the length of a string
(and any other sequence type)
The \ character after the string literal extends the string to the following line
Example:
george_v = "His majesty George v, by the grace of god " \
"of the United Kingdom of Great Britain and " \
"Ireland and of the British DOminions beyond " \
"the Seas, King, Defender of the Faith, Emperor of India"
ghandi = 'Mohandas Karamchand Ghandi'
john_f_kennedy = 'JFK'
print(len(george_v), 'characters is much too long of a name!')
print(len(ghandi), 'characters is better...
print(len(john_f_kennedy'), 'characters is short enough.')
looks like:
185 characters is much too long of a name!
26 characters is better...
3 characters is short enough.
Programs commonly access an individual character of a string. As a
sequence type, every character in a string has an index, or position, starting
,at 0 from the leftmost character. For example, the 'A' in string 'ABC' is at
index 0, 'b' is at index 1, and 'C' is at index 2. A programmer can access a
character at a specific index by appending brackets [] containing the index.
Example:
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
user_number = int(input('Enter number to use as index: '))
print()
print('\nLetter', user_number, 'of the alphabet is', alphabet[user_number])
by looking for 2, and based on the code you will see that this looks like:
Enter umber to use as index:
Letter 2 of the alphabet is C
Letter -1 of the alphabet is Z
*NOTE: negative numbers work backwards, positive work forwards
Write an expresses that accesses the first character of the string my_country
-> my_country[0]
Assign my_var with the last character in my_str. Use a negative index
my_var = my_str[-1]
,string_1 = 'abc'
string_2 = '123'
concatenated_string = string_1 + string_2
print('Easy as', + concatenated_string)
Easy as abc123
Use input() to read a string from input into a variable "person_name" Then
use int(input()) to read a value from input as an integer into variable
person_age. Ex: If the input is Amy 4, then the output is:
In 5 years Amy will be 9
Note: Do not write a prompt for the input values
CODE:
1 person_name = input()
2 person_age = int(input())
print('In 5 years', person_name, 'will be', person_age + 5)
A list = []
example:
prices = ['$20', 14.99, 5]
, Write a statement that assigns my_var with the 3rd element of my_list
my_var = my_list[2]
append() - add new elements to a list
pop() - remove element at index number
remove() - remove element
list_example = [list]
list_example.append = ('abc')
looks like: list abc
OPERATIONS AND DESCRIPTIONS:
* len(list) = find the length of the list
* list1 + list2 = produce a new list by concatenating list2 to the end of list1
* min(list) = find the element in the list with the smallest value
* max(list) = find the element in the list with the largest value
* sum(list) = find the sum of all elements of a list (numbers only)
* list.index(val) = find the index of the first element in list whose value
matches val.
list.count(val) = count the number of occurrences of the value val in list.
Write a statement that assigns the average of the elements of prices to the
variable avg_price
avg_price = sum('prices')/len(prices)
A common operation is to find the length, or the umber of characters, in a
string. The len() built-in function can be used to find the length of a string
(and any other sequence type)
The \ character after the string literal extends the string to the following line
Example:
george_v = "His majesty George v, by the grace of god " \
"of the United Kingdom of Great Britain and " \
"Ireland and of the British DOminions beyond " \
"the Seas, King, Defender of the Faith, Emperor of India"
ghandi = 'Mohandas Karamchand Ghandi'
john_f_kennedy = 'JFK'
print(len(george_v), 'characters is much too long of a name!')
print(len(ghandi), 'characters is better...
print(len(john_f_kennedy'), 'characters is short enough.')
looks like:
185 characters is much too long of a name!
26 characters is better...
3 characters is short enough.
Programs commonly access an individual character of a string. As a
sequence type, every character in a string has an index, or position, starting
,at 0 from the leftmost character. For example, the 'A' in string 'ABC' is at
index 0, 'b' is at index 1, and 'C' is at index 2. A programmer can access a
character at a specific index by appending brackets [] containing the index.
Example:
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
user_number = int(input('Enter number to use as index: '))
print()
print('\nLetter', user_number, 'of the alphabet is', alphabet[user_number])
by looking for 2, and based on the code you will see that this looks like:
Enter umber to use as index:
Letter 2 of the alphabet is C
Letter -1 of the alphabet is Z
*NOTE: negative numbers work backwards, positive work forwards
Write an expresses that accesses the first character of the string my_country
-> my_country[0]
Assign my_var with the last character in my_str. Use a negative index
my_var = my_str[-1]
,string_1 = 'abc'
string_2 = '123'
concatenated_string = string_1 + string_2
print('Easy as', + concatenated_string)
Easy as abc123
Use input() to read a string from input into a variable "person_name" Then
use int(input()) to read a value from input as an integer into variable
person_age. Ex: If the input is Amy 4, then the output is:
In 5 years Amy will be 9
Note: Do not write a prompt for the input values
CODE:
1 person_name = input()
2 person_age = int(input())
print('In 5 years', person_name, 'will be', person_age + 5)
A list = []
example:
prices = ['$20', 14.99, 5]
, Write a statement that assigns my_var with the 3rd element of my_list
my_var = my_list[2]
append() - add new elements to a list
pop() - remove element at index number
remove() - remove element
list_example = [list]
list_example.append = ('abc')
looks like: list abc
OPERATIONS AND DESCRIPTIONS:
* len(list) = find the length of the list
* list1 + list2 = produce a new list by concatenating list2 to the end of list1
* min(list) = find the element in the list with the smallest value
* max(list) = find the element in the list with the largest value
* sum(list) = find the sum of all elements of a list (numbers only)
* list.index(val) = find the index of the first element in list whose value
matches val.
list.count(val) = count the number of occurrences of the value val in list.
Write a statement that assigns the average of the elements of prices to the
variable avg_price
avg_price = sum('prices')/len(prices)