Chapter 3 : "PYTHON" (STRING'S)
Chapter 3
STRING'S
String is a data type in Python. String is a sequence of characters enclosed in quotes .We can primarily, write a string in these three ways
1. Single quoted strings (a = "Harry")2. Double quoted strings (b = "Harry")3. Trible quoted Strings (c = '!' Harry")
String Slicing:
A String in Python can be sliced for getting a part of the string .
Consider the following "string" :
name= "[Harry]" => length = 5
0 1 2 3 4
(-5)(-4)(-3)(-2) (-1)
The index in a string starts from 0 to (length-1) in Python. In order to slice a string, we use the following syntax:
- sl = name [int_start:int_end]( first inder included > cast index is not included)
- sl [0:3] returns "Har" → Charaders from 0 la 3
- sl[1:3] returns "ar^ prime - chacalins from 1 to 3
Negative Indices: Negative Indices can also be used as shown in the figure above. -1 corresponds to the (length-1) index, -2 (length -2)
Slicing with skip value :
We can provide a skip value as a part of our slice like this: 1.Word="amaging "
2.word [1:6:2] = 'man'
Other advanced slicing techniques:
word = " amazing"
word [7] → word [0.7] → 'amazing!
String functions :
Some of the mostly used functions to perform operations on or manipulate strings are:
1. Len () function → This function returns the length of the string
*Ten ("Harry") → returns 5
2 . String ends with ( r r y) → This function tells whither the variable string ends with the string "rry" or not if string is "harry", it returns true for "rry" since Harry ends with rry .
3 . String Count (c) → Counts the total number of occurence of any character
4 . siring capitalize() → This function capitalizes the first character of a given string.
5. string find (word) →This function finds a word and returns the index of first occurence of that word in the string.
6. string replace (oldword, new word)→ This function replaces the old word with new word in String
7 . Escape Sequence Characters → Sequence of characters after backslash"\" (Escape Seq characters). Escape sequence character comprises of more than one characters but represents one character when used within the strings.
Examples \n \t,
Comments
Post a Comment