String Manipulations in Python | Part - 1

05 min read

 

Addition of strings:

print('hello' + ' '+ 'FACE Prep')

Output:
hello FACE Prep

Multiplication of strings:


variable1= 'hello' *3
variable2= 'FACE Prep'

print(variable1+ variable2)

Output:
hello hello hello FACE Prep

Length of a string:


str1= 'FACE Prep'
print(len(str1))

Output:
9


"In" function: checks if variable is present in the sequence and exits a binary output, i.e True or False. For example,

fruit='banana'
'n' in fruit # returns answer True

'm' in fruit # returns answer False

#this can be used in "if" function as shown below:

if 'a' in fruit:
print('Found it')

Output:

Found it



Append:

If you want to add material to the end of a string successively,  we can append it manually by the operator +=. This is further illustrated in the following example.

str3 = 'hello'
str3 += ' '
str3 += 'FACE Prep'
print(str3)
-> hello FACE Prep
POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error