String methds

 Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32

Type "help", "copyright", "credits" or "license()" for more information.

>>> z="Aamir"

>>> type z

SyntaxError: invalid syntax

>>> type(z)

<class 'str'>

>>> z.upper()

'AAMIR'

>>> z.lower()

'aamir'

>>> len(z)

5

>>> z.replace('m','z')

'Aazir'

>>> 

>>> 

>>> names="Aamir,Asad,noor,neyaz"

>>> names

'Aamir,Asad,noor,neyaz'

>>> names.split(",")

['Aamir', 'Asad', 'noor', 'neyaz']

>>> names[0]

'A'

>>> names=names.split(",")

>>> names[0]

'Aamir'

>>> names=names.split(",")

Traceback (most recent call last):

  File "<pyshell#16>", line 1, in <module>

    names=names.split(",")

AttributeError: 'list' object has no attribute 'split'

>>> ##think you are splitting names individuallyAttributeError: 'list' object has no attribute 'split'

>>> a='5'

>>> b='10'

>>> c='15'

>>> print("The sum of +"a"+ and +"b"+ is +"z")

      

SyntaxError: invalid syntax

>>> 

>>> print("The sum of "+a+" and "+b+" is "+z+")

      

SyntaxError: EOL while scanning string literal

>>> 

>>> 

>>> 

>>> print("The sum of "+a+" and "+b+"is" +z)

The sum of 5 and 10isAamir

>>> 

>>> 

>>> print("The sum of {} and{} is{}".format(a.b.c))

Traceback (most recent call last):

  File "<pyshell#30>", line 1, in <module>

    print("The sum of {} and{} is{}".format(a.b.c))

AttributeError: 'str' object has no attribute 'b'

>>> print("The sum of {}and {} is{}".format(a.b.z))

Traceback (most recent call last):

  File "<pyshell#31>", line 1, in <module>

    print("The sum of {}and {} is{}".format(a.b.z))

AttributeError: 'str' object has no attribute 'b'

>>> print("The sum of {} and{} is {}".format(a,b,c))

The sum of 5 and10 is 15

>>> print (" The sum of {0} and{1} is {2}".format(a,b,c))

 The sum of 5 and10 is 15

>>> 

>>> 

>>> print("The sum of {1} and {0} is {2}".format(a,b,c))

The sum of 10 and 5 is 15

>>> 

Comments

Popular posts from this blog

XAMPP, SQL BASIC COMMANDS

The Minion Game Hackerrank Solution

Arrays - DS | HackerRank Solutions