KEMBAR78
Python: Strings & Intro Programming PDFs | PDF
PYTHON
Bikram Nath
Department of Computer Science
New Alipore College
Python String
String is combination of characters
S1 = “hello" S2 = "HELLO“ S3 = "HeLlo“ S4 ="Abc12“
S5 = "hello world" S6 = "123” S7 = “Today is 11.04.2023”
String with index values
Positive Index
Negative Index
String Slicing
S[ 1 ] = “e”
S[ 1 : 7 ] = “ello w”
S[ : 4] = “Hell”
S[ 4 : ] = “o World”
S[ -1] = “d”
S[ -10 : -3] = “elloWo”
S[ -10 : 9] = “elloWor”
S
S [ index ] Only one value
S [ start : stop ] Default step = 1
S [ start : stop : step ]
String functions
Functions Explanation
upper( ) Convert a string to upper case
lower( ) Convert a string to lower case
swapcase( ) Change the case of each character
capitalize( ) Change first character to upper case
title( ) Change first character of each word to upper case
S1= input( “Enter a string : “ )
S2 = S1.upper( )
print( S2 )
S1= input( “Enter a string : “ )
S2 = S1. capitalize( )
print( S2 )
S1= input( “Enter a string : “ )
S2 = S1. title( )
print( S2 )
String functions
Functions Explanation
isalpha( ) Returns true if string made with only alphabets
isdigit( ) Returns true if string made with only digits
isalnum( ) Returns true if string made with alphabets and digits
isspace( ) Returns true if string made with only space
isupper( ) Returns true if string is in upper case
islower( ) Returns true if string is in lower case
S= input( “Enter a string : “ )
x = S.isalpha( )
print( x )
S= input( “Enter a string : “ )
x = S.isalpha( )
print( x )
String functions
Functions Explanation
find( ) Checks a given substring is present or not. It may have start & stop position
count( ) Counts the number of times substring present
replace ( ) Replace first string with second string
partition( ) Partition the string into list by given substring
split ( ) Split the string according the substring given. Default split by space.
S1 = input( "Enter a string : " )
x = S1.find( “o" )
print( x )
S1 = input( "Enter a string : " )
x = S1.find( "p" )
print( x )
String functions
Functions Explanation
find( ) Checks a given substring is present or not. It may have start & stop position
count( ) Counts the number of times substring present
replace ( ) Replace first string with second string
partition( ) Partition the string into list by given substring
split ( ) Split the string according the substring given. Default split by space.
S1 = input( "Enter a string : " )
x = S1.count( “o" )
print( x )
S1 = input( "Enter a string : " )
x = S1.count( "p" )
print( x )
String functions
Functions Explanation
find( ) Checks a given substring is present or not. It may have start & stop position
count( ) Counts the number of times substring present
replace ( ) Replace first string with second string
partition( ) Partition the string into list by given substring
split ( ) Split the string according the substring given. Default split by space.
S1 = input ( "Enter a string : " )
x = S1.replace( “o“ , “a” )
print( x )
S1 = input ( "Enter a string : " )
x = S1.partition( “o" )
print( x )
String functions
Functions Explanation
find( ) Checks a given substring is present or not. It may have start & stop position
count( ) Counts the number of times substring present
replace ( ) Replace first string with second string
partition( ) Partition the string into list by given substring
split ( ) Split the string according the substring given. Default split by space.
S1 = input ( "Enter a string : " )
x = S1.split( )
print( x )
S1 = input ( "Enter a string : " )
x = S1.split( “o" )
print( x )
String programs
1. Write a program to input a string from user and print each character in separate line
2. Write a program to input a string from user and print each word in separate line
3. Write a program to input a string and a substring from user and print each number of
words present in the string.
Thank You

Python: Strings & Intro Programming PDFs

  • 1.
    PYTHON Bikram Nath Department ofComputer Science New Alipore College
  • 2.
    Python String String iscombination of characters S1 = “hello" S2 = "HELLO“ S3 = "HeLlo“ S4 ="Abc12“ S5 = "hello world" S6 = "123” S7 = “Today is 11.04.2023” String with index values Positive Index Negative Index
  • 3.
    String Slicing S[ 1] = “e” S[ 1 : 7 ] = “ello w” S[ : 4] = “Hell” S[ 4 : ] = “o World” S[ -1] = “d” S[ -10 : -3] = “elloWo” S[ -10 : 9] = “elloWor” S S [ index ] Only one value S [ start : stop ] Default step = 1 S [ start : stop : step ]
  • 4.
    String functions Functions Explanation upper() Convert a string to upper case lower( ) Convert a string to lower case swapcase( ) Change the case of each character capitalize( ) Change first character to upper case title( ) Change first character of each word to upper case S1= input( “Enter a string : “ ) S2 = S1.upper( ) print( S2 ) S1= input( “Enter a string : “ ) S2 = S1. capitalize( ) print( S2 ) S1= input( “Enter a string : “ ) S2 = S1. title( ) print( S2 )
  • 5.
    String functions Functions Explanation isalpha() Returns true if string made with only alphabets isdigit( ) Returns true if string made with only digits isalnum( ) Returns true if string made with alphabets and digits isspace( ) Returns true if string made with only space isupper( ) Returns true if string is in upper case islower( ) Returns true if string is in lower case S= input( “Enter a string : “ ) x = S.isalpha( ) print( x ) S= input( “Enter a string : “ ) x = S.isalpha( ) print( x )
  • 6.
    String functions Functions Explanation find() Checks a given substring is present or not. It may have start & stop position count( ) Counts the number of times substring present replace ( ) Replace first string with second string partition( ) Partition the string into list by given substring split ( ) Split the string according the substring given. Default split by space. S1 = input( "Enter a string : " ) x = S1.find( “o" ) print( x ) S1 = input( "Enter a string : " ) x = S1.find( "p" ) print( x )
  • 7.
    String functions Functions Explanation find() Checks a given substring is present or not. It may have start & stop position count( ) Counts the number of times substring present replace ( ) Replace first string with second string partition( ) Partition the string into list by given substring split ( ) Split the string according the substring given. Default split by space. S1 = input( "Enter a string : " ) x = S1.count( “o" ) print( x ) S1 = input( "Enter a string : " ) x = S1.count( "p" ) print( x )
  • 8.
    String functions Functions Explanation find() Checks a given substring is present or not. It may have start & stop position count( ) Counts the number of times substring present replace ( ) Replace first string with second string partition( ) Partition the string into list by given substring split ( ) Split the string according the substring given. Default split by space. S1 = input ( "Enter a string : " ) x = S1.replace( “o“ , “a” ) print( x ) S1 = input ( "Enter a string : " ) x = S1.partition( “o" ) print( x )
  • 9.
    String functions Functions Explanation find() Checks a given substring is present or not. It may have start & stop position count( ) Counts the number of times substring present replace ( ) Replace first string with second string partition( ) Partition the string into list by given substring split ( ) Split the string according the substring given. Default split by space. S1 = input ( "Enter a string : " ) x = S1.split( ) print( x ) S1 = input ( "Enter a string : " ) x = S1.split( “o" ) print( x )
  • 10.
    String programs 1. Writea program to input a string from user and print each character in separate line 2. Write a program to input a string from user and print each word in separate line 3. Write a program to input a string and a substring from user and print each number of words present in the string.
  • 11.