Python Strings Methods, Concatenation, & Repetition
you don't have to use repetition with just single character strings, for ex:
happiness = 'happy ' * 3 print(happiness) Output: happy happy happy
this method of a string object returns a copy of the string in all lower case letters:
lower() method
Objects also have functions, but they are not called functions. They are called this:
methods
string repetition example:
print ('-' * 10) Output: ----------
string concatenation example w/ spaces:
print('I ' + 'love ' + 'Python.') print('I' + ' love' + ' Python.') Output: I love Python. I love Python.
string concatenation example w/o spaces:
print('I' + 'love' + 'Python.') Output: IlovePython.
'apple' is an object with a type of "str," which is short for this.
short for string.
When working with strings, this symbol is the repetition operator for repeating strings.
the asterisk symbol (*)
the string repetition format is:
the format is: 'string' * number_of_times_to_repeat
To concatenate, or combine, two strings use this math sign:
the plus sign (+)
this string method returns a copy of the string in all upper case letters:
the upper() string method
if we assign the value of apple to the variable fruit using fruit = 'apple', then fruit is also this.
then fruit is also a string object
to concatenate:
to combine
t/f: everything in Python is an object, and every object has a type.
true
variables are names that represent this:
values
Methods are functions that are run against this.
(that are run against) an object
OOP stands for this:
Object Oriented Programming, (OOP)
this is a section of reusable code that performs an action:
a function
t/f: to call a method on an object, follow the object with a period, then the method name, and finally a set of parenthesis . Enclose any parameters in the parenthesis.
ex: (the lower string method) fruit= 'Apple' print(fruit.lower()) Output: apple
t/f: one can NOT concatenate multiple strings by using additional plus signs and strings.
false: one CAN concatenate multiple strings by using additional plus signs and strings.
The following example demonstrates string concatenation using variables.
first = 'I' second = 'love' third = 'Python' sentence = first + ' ' + second + ' ' + third + '.' print(sentence) Output: I love Python.
lower() method example:
fruit = 'Apple' print(fruit.lower()) Output: apple
an upper() string method example:
fruit = 'Apple' print(fruit.upper()) Output: APPLE