Difference between revisions of "String Manipulation - Python"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "=Why String Manipulation= All programming languages allow you to manipulate strings, it is an essential skill and function. You could use it to find a value within a string, u...")
(No difference)

Revision as of 09:30, 20 June 2018

Why String Manipulation

All programming languages allow you to manipulate strings, it is an essential skill and function. You could use it to find a value within a string, use it to select only a part of the string and so on.

String Manipulation in Python

str = 'Hello World!' 
print (str) # Prints complete string 

print (str[0]) # Prints first character of the string, or any character at the element provided so str[3] will get the 4th character 

print (str[2:5]) # Prints characters starting from 3rd to 5th, this allows you to select a part of a string 

print (str[2:]) # Prints string starting from 3rd character, you could also use str[:2] to get the upto the 3rd character 

print (str * 2) # Prints string two times 

print (str + "TEST“) # Prints concatenated string