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...")
 
(String Manipulation in Python)
 
(3 intermediate revisions by the same user not shown)
Line 4: Line 4:
 
=String Manipulation in Python=
 
=String Manipulation in Python=
  
 +
===Print a String===
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>
 
str = 'Hello World!'  
 
str = 'Hello World!'  
 
print (str) # Prints complete string  
 
print (str) # Prints complete string  
 +
</syntaxhighlight>
 +
 +
===Get the length of a String===
 +
<syntaxhighlight lang=python>
 +
str = 'Hello World!'
 +
print ( len(str) ) # len() gets the length of a string
 +
</syntaxhighlight>
  
 +
===Get a Character from a String===
 +
<syntaxhighlight lang=python>
 +
str = 'Hello World!'
 
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[0]) # Prints first character of the string, or any character at the element provided so str[3] will get the 4th character  
 +
</syntaxhighlight>
  
 +
===Get a Selection of the String from the Middle===
 +
<syntaxhighlight lang=python>
 +
str = 'Hello World!'
 
print (str[2:5]) # Prints characters starting from 3rd to 5th, this allows you to select a part of a string  
 
print (str[2:5]) # Prints characters starting from 3rd to 5th, this allows you to select a part of a string  
 +
</syntaxhighlight>
  
print (str[2:]) # Prints string starting from 3rd character, you could also use str[:2] to get the upto the 3rd character  
+
===Get a Selection of the String from the Start or End===
 +
<syntaxhighlight lang=python>
 +
str = 'Hello World!'
 +
print (str[2:]) # Prints string starting from 3rd character until the end
 +
print (str[:2]) # Prints string starting from the start and upto the 3rd character
 +
</syntaxhighlight>
  
 +
===Repeating String===
 +
<syntaxhighlight lang=python>
 +
str = 'Hello World!'
 
print (str * 2) # Prints string two times  
 
print (str * 2) # Prints string two times  
 +
</syntaxhighlight>
  
print (str + "TEST“) # Prints concatenated string  
+
===String Concatenation===
 +
<syntaxhighlight lang=python>
 +
str = 'Hello World!'
 +
print (str + "TEST") # Prints concatenated string  
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
=Now Try=
 +
Create a program to count the number of vowels in a given sentence or string
 +
* Ask the user to enter a sentence or string
 +
* Create a for loop to cycle through each character (hint test[0] will access the first element of a string called test)
 +
* Create the if statements to count number of A, E, I, O, & U's
 +
* Display a message to say how many of each vowel is present

Latest revision as of 12:29, 4 March 2019

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

Print a String

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

Get the length of a String

str = 'Hello World!' 
print ( len(str) ) # len() gets the length of a string

Get a Character from a String

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

Get a Selection of the String from the Middle

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

Get a Selection of the String from the Start or End

str = 'Hello World!' 
print (str[2:]) # Prints string starting from 3rd character until the end
print (str[:2]) # Prints string starting from the start and upto the 3rd character

Repeating String

str = 'Hello World!' 
print (str * 2) # Prints string two times

String Concatenation

str = 'Hello World!' 
print (str + "TEST") # Prints concatenated string

Now Try

Create a program to count the number of vowels in a given sentence or string

  • Ask the user to enter a sentence or string
  • Create a for loop to cycle through each character (hint test[0] will access the first element of a string called test)
  • Create the if statements to count number of A, E, I, O, & U's
  • Display a message to say how many of each vowel is present