Professional Documents
Culture Documents
STRING CONCATENATION
In here, we will add one string to the end of another.
This concept is just one of many ways to add string variables
together to complete a larger string.
using the addition operator without variables:
47 48 By: JMI
This formatting method is applicable to python 3.6 to present This formatting method use percent operators (%) to mark the location of
The new way to insert variables into a string in Python is by using what we the variable being inserted.
call f strings. The letter after the percent operator signifies data type.
By putting the letter “f” in front of a string, you’re able to insert a variable Use %s to insert strings and %d for digit
into a string directly in line. After the string closes, you would place a percent operator, followed by the
To insert a variable in a string, simply wrap curly brackets around the name variables you would like to use.
of the variable. using the % operator for single using the % operator for single number
using the new f strings string variables formatting variables formatting
sample code:
name = "John" sample code: sample code:
print( f"Hello {name}" ) name = 'Jeanne' age = 35
print('Hello, %s' % name) print('John is %d years old. ' % age)
using the % operator for multiple string variables formatting When a computer saves a string into memory, each character within the string is
assigned what we call an “index.” An index is essentially a location in memory.
sample code:
first_name = "Jeanne" For example, we take a string like “Hello” and break down their indexes, we can see
last_name = " Imbuido" that the letter “H” is located at index zero, refer to sample code:
print( "Hello, %s %s." % (first_name, last_name) ) using # using indexes to print each element
# Enclose the variables in parenthesis sample code:
word = "Hello"
using the % operator for multiple number variables formatting print( word[ 0 ] ) # will output 'H'
print( word[ 1 ] ) # will output 'e'
sample code: print( word[ -1 ] ) # will output 'o'
b =9 In order to index a specific element, you use square brackets to the right of the
l=7 variable name. Within those square brackets, you put the index location you wish to
p=2 access.
a= " Saint Agatha Homes, Tikay, Malolos City "
print(‘ I am currently residing at Blk.%d Lot %d Phase %d %s. ' % (b, l, p, a) ) Using negative index numbers will result in trying to access information from the
back, such that -4 would result in the output of the letter “e”.
51 By: JMI 52 By: JMI
using # using indexes to print each Slicing is used mostly with Python lists, however, you can use it on strings as well.
element String slicing is essentially when you only want a piece of the variable.
sample code:
for example, if we only wanted “He” from the word “Hello”,
word = “Jeanne"
print( word[ 0 ] ) # will output ‘J'
print( word[ 1 ] ) # will output 'e‘ Will output “He” only:
print( word[ 2 ] ) # will output ‘a'
print( word[ 3 ] ) # will output ‘n' sample code:
print( word[ 4 ] ) # will output ‘n‘ print( word[ 0 : 2 ] ) # will output 'He'
print( word[ 5 ] ) # will output 'e‘ print( word[ 1 : 4 ] ) # will output ‘ell'
print( word[ 6 ] ) # will crashed your
program
The first number in the bracket is the starting index; the second is the stopping
index (Note: this always starts counting from the beginning letter.
53 By: JMI 54 By: JMI