String Random Round
String Random Round
var1[0]: N
var2[1:5]: ytho
var1 = 'Hello World!’
print "Updated String :- ", var1[:6] +
'Python'
output
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
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
• 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))
import random
print(random.randint(0,9))