4. String in Python

String: A contiguous set of characters which is represented in quotation marks.

str1="Welcome to Second research practice python class"

print(str1)

Output:

Welcome to Second research practice python class

print("First Character of String ", str1[0])

Output: First Character of String W

print(" String from 11th to 17th ", str1[11:17])

Output: String from 11th to 17th Second

print(" String string from 11th ", str1[11:])

output: String string from 11th Second research practice python class

#Printing String Two Times

print(str1*2)

Output: Welcome to Second research practice python classWelcome to Second research practice python class

prints concatenated string

print(str1+ " at BITS Pilani Dubai Campus")

Output: Welcome to Second research practice python class at BITS Pilani Dubai Campus

Escape Characters in Python:

\a : bell or alert

\b: backspace

\f: formfeed

\n : newline

\r: carriage return

\s: space

\t: tab

\v: vertical tab

String Frmatting Operator

%c : character

%s : st ring

%i or %d : singed decimal integer

%u: unsigned decimal integer

%o : octal integer

%x: hexademical

%e: explonetinal number

%f: floating point real number

print("%o is the octal octal equivalent of %d" %(8,8))

output: 10 is the octal octal equivalent of 8

String Functions

  1. len(string)

  2. lower()

  3. upper()

  4. swapcase()

  5. capitalize()

  6. title()

  7. lstrip()

  8. rstrip()

  9. strip()

  10. max(string)

  11. min(string)

  12. replace(old, new)

  13. center(width, fillchar)

  14. ljust(widhth,[,fillchart])

  15. rjust(width,[fillchar])

  16. zfill(width)

  17. count(str, beg=0, end=len(string)

  18. find(str, beg, end)

  19. rfind()

  20. index(str, beg=0, end=len(str)

  21. startwith()

  22. endwith()

  23. isdecimal()

  24. isalpha()