0% found this document useful (0 votes)
8 views

String Random Round

Uploaded by

iamkyros
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

String Random Round

Uploaded by

iamkyros
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

String Handling

IGCSE Computer Science


String
• Used to store text(combination of letters,digits ,special chaacters etc)
• It can be empty(having no characters ) or any numbers of characters
can be there
• The first character in a string can be at position 0 or 1,depending on
programming language
Strings in Python
• We can create Strings in python language by simply enclosing
characters in quotes.
• Python treats single quotes’ ‘ the same as double quotes” “.
• Eg
var1 = 'Hello World!’
var2 = "Python
Programming"
• Python does not support a character type(char datatype); these
are treated as strings of length one, thus also considered a
substring.
var1 = “Neerja Modi School”
var2 = "Python Programming"
print "var1[0]: ", var1[0]
print "var2[1:5]: ", var2[1:5]
Output of previous code snippet

var1[0]: N
var2[1:5]: ytho
var1 = 'Hello World!’
print "Updated String :- ", var1[:6] +
'Python'
output

Updated String :- Hello Python


SubString
• Extracting part of a string for example string “science” can be
extracted from “computer science”
• Python offers many ways to substring a string. This is
often called "slicing".

string[start:end:step]
string[start:end] Get all characters from start to end - 1

string[:end] Get all characters from the beginning of the string to end-1

string[start:] Get all characters from start to the end of the string

ring[start:end:step]: Get all characters from start to end - 1, not including every step charact
Python String islower() Method
The islower() method returns True if all the characters are in lower case, otherwise False.
Numbers, symbols and spaces are not checked, only alphabet characters.
Python String lower() Method

The lower() method returns a string where all characters are lower case.
Symbols and Numbers are ignored.
Python String isupper() Method

The isupper() method returns True if all the characters are in upper case, otherwise False.
Numbers, symbols and spaces are not checked, only alphabet characters.
Python String upper() Method

The upper() method returns a string where all characters are in upper case.
Symbols and Numbers are ignored.
Finding LENGTH (no of characters )
in a string
• Using the built-in function len. The built-in function len
returns the number of items in a container.

• str = "geeks"
• print(len(str))
# Python code to demonstrate string length
# using for loop

Str=“Neerja Modi School”


for i in str:
counter += 1

print(counter)
Python String programs
1. Write a Python program to calculate the length of a string
2. Python Program to count the number of vowels in a
string
3. Write a Python program to get a string made of the first 2 and
the last 2 chars from a given a string.
string=raw_input("Enter string:") #raw_input reads string only
Vowels=0
for i in string:
if(i=='a' or i=='e' or i=='i' or i=='o' or i=='u' or i=='A' or i=='E' or i=='I' or i=='O' or
i=='U’):
vowels=vowels+1

print("Number of vowels are:")


print(vowels)
• str='hello python programmers'
• str1=str[0:2]
• str2=str[-2:]

• news=str1+str2
• print(news)

• Output : hers
Rounding a Number in
python
• x = round(5.76543) # round to nearest integer
• print(x)

Output : 6
Round a number to 2 decimal places
• x = round(5.76543, 2)
print(x)

• Output 5.77
Analyze the output of following
• # for integers
• print(round(15))

• # for floating point


• print(round(51.6))
• print(round(51.5))
• print(round(51.4))
Random number generations in
python

# Program to generate a random number between 0 and 9


# importing the random module

import random

print(random.randint(0,9))

You might also like