day4

Pramod Ray
2 min readSep 7, 2019

--

String in Python : String in python are surrounded by either single quotation marks, or double quotation marks. or

strings can be created by enclosing the character or the sequence of characters in the quotes. Python allows us to use single quotes, double quotes, or triple quotes to create the string. Ex-

print("Hello, World")

Output :- Hello World

String Operators

  • + : It is known as concatenation operator used to join the strings given either side of the operator.
  • * : It is known as repetition operator. It concatenates the multiple copies of the same string.
  • [] : It is known as slice operator. It is used to access the sub-strings of a particular string.
  • [:] : It is known as range slice operator. It is used to access the characters from the specified range.
  • in : It is known as membership operator. It returns if a particular sub-string is present in the specified string.
  • not in : It is also a membership operator and does the exact reverse of in. It returns true if a particular substring is not present in the specified string.
  • r/R : It is used to specify the raw string. Raw strings are used in the cases where we need to print the actual meaning of escape characters such as “C://python”. To define any string as a raw string, the character r or R is followed by the string.
  • % : It is used to perform string formatting. It makes use of the format specifiers used in C programming like %d or %f to map their values in python. We will discuss how formatting is done in python.

Concatenation :- Add two String, using (+) operator

first_name="Pramod"  last-name="Roy"
Surename=first-name+" "+last_name
print(Surename)

Output :- Pramod Ray

Repetition :- Creates new string, concatenating multiple copies of the same string. ex-

str= _*2;

Slice :- Gives the character from the given indes

input[start:end:step]
str="Python"
str[0:1]

Output:- P

Note :- [start:end:step] support only integer not complex and string

String Length() : - Python has a set of built-in methods that you can use on strings.

str="Pramod"
print(len(str))

Output :- 6

--

--

Pramod Ray
Pramod Ray

No responses yet