CCC: coding crash course (3)

Various uses of strings


#Define a string
A = 'Ichiro'

print(A)

#String concatenation

B = "Suzuki" 

C = A+B

print(C)

#Repeat string.

say = "This is important."

say_two   = say*2
say_three = say*3

print(say)
print(say_two)
print(say_three)

print("-"*20)

#Indicates the length.

print( len(say) )
print( len(say_two) )
print( len(say_three) )

print("-"*20)


#Search for strings

D = "My name is Hideki Matsui."

print( "Is the word *is* included in D?")
print( "is" in D )


print( "Is the word *are* included in D?")
print( "are" in D )

print("-"*20)


#Examine the element

print(D[0])

print(D[5])

print(D[10])

print(D[15])

print(D[5:15]) # slice

Various uses of list (1)


A = [1,2,3,4,5]


print(A[0])

print(A[2])

print(A[-1])

print(A[1:3]) # "slice"

print(A)


B = [100,101,102]


print( A + [100,101,102] )

print( A + [100,] )

print( A + [100] ) 

print(A)

#I can't do this:
# print(A + 100) 

#You can also use append.
A.append(100)

#There is also an extend.
A.extend([200,201,202])

print(A)


print('-'*20)


print(A)
print(A[2])

A[2] = 2020

print(A)
print(A[2])

print('-'*20)

del A[3] 

print(A)

print(A[3])


print('-'*20)


print( 1 in A )

print( 2 in A )

print( 3 in A )

print( 4 in A )

print( 5 in A )

print('++'*20)

print( 100 in A )

print( 101 in A )


print( 102 in A )


print('=='*20)

print( 2020 in A )

print('..'*20)


You can check the position (address) of the element in the list with the index method.


list_names = ['Tom', 'Jack', 'Bob', 'Andy']

print(list_names.index('Jack'))

print(list_names.index('Andy'))

print(list_names.index('ANDY')) 
#This doesn't work. This will cause an error, so avoid it by using an if statement or the like.


if ( 'ANDY' in list_names):
    print( list_names.index('ANDY') )
else : 
    print( 'ANDY is an unknown name.')


if ( 'Jack' in list_names):
    print( list_names.index('Jack') )
else : 
    print( 'Jack is an unknown name.')





Recommended Posts

CCC: coding crash course (1)
CCC: coding crash course (3)
CCC: coding crash course (2)
CCC: coding crash course (4) Make the numbers appearing in pi 3.141562 .... into a histogram
CCC: coding crash course (5) Find out the frequency of words and letters that appear in Steve Jobs speeches