String Operations
You can add two string (concatentate):
helloworld = "hello" + " " + "world"
print(helloworld)
number = 12
print (helloworld + " " + str(number))
print(helloworld[6])
print(helloworld[3:])
Just press Run
!
You can split strings:
helloworld = "Hi there Modeling and Simulation Students"
word_list = helloworld.split()
print(word_list)
x = "blue,red,green"
print(x.split(","))
a,b,c = x.split(",")
print(b)
Just press Run
!
Python strings have the strip() and replace() method for removing any character from a string.
If the characters to be removed are not specified then white-space will be removed.
helloworld = "Hi there Modeling and Simulation Students"
print(helloworld.strip('H')) # remove the trailing H- note: strip in Python has changed from 3.6 on.
print(helloworld.replace(' ','_')) # replace the space with _
Just press Run
!
List Operations
You can add two lists (concatentate):
even_numbers = [2,4,6,8]
odd_numbers = [1,3,5,7]
all_numbers = odd_numbers + even_numbers
print(all_numbers)
Just press Run
!
Python supports forming new lists with a repeating sequence using the multiplication operator:
print([1,2,3] * 3)
Just press Run
!