Q.1 what is string. How we concatenate two strings.
1. Python string data type is a sequence of characters.
2. Characters could be a letter, digit, whitespace or any other symbol.
3. Python treats strings as contiguous series of characters delimited/enclosed by single, double or triple quotes.
4. Python has built in string class str
5. We can declare and define a string by creating a variable of string type.
6. str() is used to convert values of any other type into string type.
7. Example-
name=“India”
country=name
division=str(“FE-G”)
Q.2 Explain String indexing and string Traversing with Give Example.
String Indexing and String Traversing-
String Indexing-
1. Individual characters in a string are accessed using the subscript ([]) operator.
2. The expression in bracket called index
3. Index specifies the character we want to access from given set of characters in a string.
4. Index of first character is 0 and index of last character is n-1. nis number of character in string.
5. Every symbol in string having its own index.
String Traversing-
A string can be traversed by accessing characters from one index to another.
Q.3 write a note on string format operator.
String Format Operator %
1. The format operator, % allows users to construct strings, replacing parts of the strings with data stored in variables.
2. The % operator takes a format string on left (%d, %s) and the corresponding values in tuple on the right.
3. Multiple values added in parenthesis and separated by comma.
, Following are the format sequence-
i. %s - String (or any object with a string representation, like numbers)
ii. %c- single character
iii. %d or %i – Integers
iv. %u- unsigned decimal integer
v. %f - Floating point numbers
vi. %.<number of digits>f - Floating point numbers with a fixed amount of digits to the right of the dot.
vii. %o- Octal integer
viii. %x - Integers in hex representation
Syntax and Example of String Formating operator %
“<format>” %(<values>)
Format consist of sequence of character and conversion specification.
Conversion specification start with a % operator and can appear anywhere within a string.
Example- Program to use format sequence while printing a string
name=“abcd”
rollno=35
print(“name is %s and rollno is %d” %( name, rollno))
output- name is abcd and rollno is 35
Q.4 Explain use of format() method with help of example.
Format() method and { } placeholder
1. The format function used with strings is very powerful function used for formatting string.
2. Format string have curly braces { } as placeholder which get replaced.
3. We can use positional argument or keyword arguments to specify the order of fields which have to be replace.
4. Example-
a="welcome“
b="python“
print("{ } to learning { }".format( a, b))
print("{b} for learning {a}".format(b = "easy", a = "language“ ))
OUTPUT-
welcome to learning python
easy for learning language
Q.5 What is slice operation? Explain with example.