SlideShare a Scribd company logo
PYTHON PROGRAMMING
UNIT II - STRINGS
Dr. D.Kavitha
STRINGS
 A string consists of a sequence of characters, which includes letters, numbers, punctuation marks and spaces.
 To represent strings, you can use a single quote, double quotes or triple quotes.
 Creating and Storing Strings
 single_quote = 'This is a single message'
 2. >>> double_quote = "Hey it is my book"
 3. >>> single_char_string = "A"
 4. >>> empty_string = ""
 5. >>> empty_string = ''
 6. >>> single_within_double_quote = "Opportunities don't happen. You create them.“
 8. >>> same_quotes = 'I've an idea'
 9. >>> triple_quote_string = '''This
 … is
 … triple
 … quote'''
STRINGS
 A single character is also treated as string.
 A string does not need to have any characters in it - empty strings.
 A string enclosed in double quotation marks can contain single quotation marks
 if one type of quotation mark surrounds the string, you have to use the other type
within it.
 If you want to include the same quotation marks within a string as you have used
to enclose the string, then you need to preface the inner quote with a backslash.
 If you have a string spanning multiple lines, then it can be included within triple
quotes.
 All white spaces and newlines used inside the triple quotes are literally reflected
in the string
 You can find the type of a variable by passing it as an argument to type() function.
Python strings are of str data type .
STRINGS
 The str() Function
 The str() function returns a string which is considered an informal or nicely printable
 representation of the given object. The syntax for str() function is, str(object)
 It returns a string version of the object. If the object is not provided, then it returns an
 empty string.
 1. >>> str(10)
 '10'
 2. >>> create_string = str()
 3. >>> type(create_string)
 <class 'str'>
 Here integer type is converted to string type. Notice the single quotes to represent the
 string . The
➀ create_string is an empty string of type
➁ str .
➂
Basic String Operations
 + - operator is used to concatenate strings. Cannot use the + operator to concatenate
values of two different types
 1. >>> string_1 = "face"
 2. >>> string_2 = "book"
 3. >>> concatenated_string = string_1 + string_2
 4. >>> concatenated_string
 'facebook‘
 singer = 50 + "cent"
 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 TypeError: unsupported operand type(s) for +: 'int' and 'str'
 8. >>> singer = str(50) + "cent"
 9. >>> singer
 '50cent'
Basic String Operations
 * - operator is used to create a repeated sequence of strings.
 >>> rep_of_string = "wow" * 5
 >>> rep_of_string
 ‘wowwowwowwowwow’
 presence of a string in another string using in and not in membership
operators.
 1. >>> fruit_string = "apple is a fruit"
 2. >>> fruit_sub_string = "apple"
 3. >>> fruit_sub_string in fruit_string
 True
 A substring is any sequence of characters that is contained in a string.
String Comparison
 Strings can be compared using various comparison operators (>, <, <=, >=, ==, !=)
 Python compares strings using ASCII value of the characters.
 1. >>> "january" == "jane"
 False
 2. >>> "january" != "jane"
 True
 3. >>> "january" < "jane"
 False
 4. >>> "january" > "jane"
 True
 5. >>> "filled" > ""
 True
Built-In Functions Used on Strings
 Sample Built-In Functions and Description
 len() - The len() function calculates the number of characters in a string. The white
space characters are also counted.
 max() - The max() function returns a character having highest ASCII value in given string.
 min() - The min() function returns character having lowest ASCII value.
 1. >>> count_characters = len("eskimos")
 2. >>> count_characters
 7
 3. >>> max("axel")
 'x'
 4. >>> min("brad")
 'a'
Accessing Characters in String by Index Number
 Each character in the string occupies a position in the string.
 The first character is at index 0; the next character is at index 1, and so on.
 The length of a string is the number of characters in it.
 You can access each character in a string using a subscript operator i.e., a square bracket.
 Square brackets are used to perform indexing in a string to get the value at a specific index or
position. This is also called subscript operator.
 string_name[index]
 index is usually in the range of 0 to len-1 or - from the end of the string starting from an
index number of −1(negative indexing)
 1. >>> word_phrase = "be yourself”
 2. >>> word_phrase[3] >>> word_phrase[-1]
 ‘y’ 'f'
Accessing Characters in String by Index Number
String Slicing and Joining
 A substring is created when slicing the strings . "slice" syntax : string_name[start:end[:step]]
 1. >>> healthy_drink = "green tea"
 2. >>> healthy_drink[0:3] >>> healthy_drink[-3:-1]
 'gre‘ 'te'
 3. >>> healthy_drink[:5] >>> healthy_drink[6:-1]
 'green‘ 'te'
 4. >>> healthy_drink[6:] >>> healthy_drink[::3]
 'tea‘ ‘get’
 5. >>> healthy_drink[:]
 'green tea'
 6. >>> healthy_drink[4:4]
 ‘’ empty string
 7. >>> healthy_drink[6:20]
 'tea'
String Slicing
 Write Python Code to Determine Whether the Given String Is a Palindrome or Not Using Slicing
 def main():
 user_string = input("Enter string: ")
 if user_string == user_string[::-1]:
 print(f"User entered string is palindrome")
 else:
 print(f"User entered string is not a palindrome")
 if __name__ == "__main__":
 main()
 Output
 Case 1:
 Enter string: madam
 User entered string is palindrome
Joining Strings Using join() Method
 join() - concatenate strings.
 Syntax: string_name.join(sequence)
 sequence can be string or list.
 If the sequence is a string, then join() function inserts string_name
between each character of the string sequence and returns the
concatenated string.
 If the sequence is a list, then join() function inserts string_name between
each item of list sequence and returns the concatenated string.
 It should be noted that all the items in the list should be of string type.
Joining Strings Using join() Method
1. >>> date_of_birth = ["17", "09", "1950"]
2. >>> ":".join(date_of_birth)
'17:09:1950'
3. >>> social_app = ["instagram", "is", "an", "photo", "sharing",
"application"]
4. >>> " ".join(social_app)
'instagram is an photo sharing application'
5. >>> numbers = "123"
6. >>> characters = "amy"
7. >>> password = numbers.join(characters)
8. >>> password
'a123m123y'
Split Strings Using split() Method
 The split() method returns a list of string items by breaking up the string using the delimiter
string. The syntax of split() method is string_name.split([separator [, maxsplit]])
 Separator - based on the specified separator, given string is split into list of strings,
 Maxsplit - at most maxsplit splits are done, no limit – not specified or −1
 1. >>> inventors = "edison, tesla, marconi, newton"
 2. >>> inventors.split(",")
 ['edison', ' tesla', ' marconi', ' newton']
 3. >>> watches = "rolex hublot cartier omega"
 4. >>> watches.split()
 ['rolex', 'hublot', 'cartier', 'omega']
Strings Are Immutable
 strings are immutable, it cannot be modified
 can assign different string values to the same string variable.
 1. >>> stre = "dollar"
 2. >>> stre[0] = "c"
 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 TypeError: 'str' object does not support item assignment
 3. >>> str1 = "c" + stre[1:]
 4. >>> str1
 'collar'
 5. >>> stre = "rollar"
 6. >>> stre
 'rollar'
String Traversing
 Characters of a string can be traversed using the for loop.
 Write Python Program to Calculate the Length of a String Without Using Built-In len() Function
 def main():
 user_string = input("Enter a string: ")
 length = 0
 for chr in user_string:
 length += 1
 print(f"The length of user entered string is {length} ")
 if __name__ == "__main__":
 main()
String Methods
 1. >>> dir(str)
 ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__',
 '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__',
 '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__
 mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
 '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize',
 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format',
 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower',
 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip',
 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip',
 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
 Various methods associated with str are displayed
Formatting Strings
 %-formatting
 str.format()
 f-strings - formatted strings
 %-formatting - int, str and doubles can be formatted. Pass only single value.
 %s - String (or any object with a string representation, like numbers)
 %d - Integers
 %f - Floating point numbers
 %.<number of digits>f - Floating point numbers with a fixed amount of digits to the right of
the dot.
 %x/%X - Integers in hex representation (lowercase/uppercase)
 >>>import math
 >>> print('The value of pi is approximately %5.3f.' % math.pi)
 >>> The value of pi is approximately 3.142.
Str.format()
 The syntax for format() method is,
 str.format(p0, p1, ..., k0=v0, k1=v1, ...) where p0, p1,... are called as
positional arguments and, k0, k1,... are keyword arguments with their
assigned values of v0, v1,... respectively.
 Positional arguments are a list of arguments that can be accessed with an
index of argument inside curly braces like {index}. Index value starts from
zero.
 Keyword arguments are a list of arguments of type keyword = value, that
can be accessed with the name of the argument inside curly braces like
{keyword}.
 Here, str is a mixture of text and curly braces of indexed or keyword types
Str.format()
 1. country = input("Which country do you live in?")
 2. print("I live in {0}".format(country))
 Output
 Which country do you live in? India
 I live in India
 1. a = 10
 2. b = 20
 3. print("The values of a is {0} and b is {1}".format(a, b))
 4. print("The values of b is {1} and a is {0}".format(a, b))
 Output
 The values of a is 10 and b is 20
 The values of b is 20 and a is 10
 1. >>> print("Give me {ball} ball".format(ball = "tennis"))
 Give me tennis ball
F’string
 f'string_statements {variable_name [: {width}.{precision}]}’
 print(f'{name:10} ==> {phone:10d}') ...
 >>> f'result: {value:{width}.{precision}}'
Escape Sequences
 Escape Sequences are a combination of a backslash () followed by either a letter or a combination
of letters and digits. Escape sequences are also called as control sequences.
 The backslash () character is used to escape the meaning of characters that follow it by
substituting their special meaning with an alternate interpretation.
Raw Strings
 A raw string is created by prefixing the character r to the string. In Python, a raw string ignores all
types of formatting within a string including the escape characters.
 1. >>> print(r"Bible Says, "Taste and see that the LORD is good; blessed is the man who takes refuge
in him."")
 Bible Says, "Taste and see that the LORD is good; blessed is the man who takes refuge in him.
 Unicodes
 Unicode Standard provides a unique number for every character, no matter what platform, device
application or language. Ex: UTF-8, UTF-16, and UTF-32
 Regular Python strings are not Unicode: they are just plain bytes. To create a Unicode
 string, use the 'u' prefix on the string literal. For example,
 1. >>> unicode_string = u'A unicode u018e string xf1'
 2. >>> unicode_string
 'A unicode string ñ'

More Related Content

PDF
Strings3a4esrxdfgcbhjjjjjiiol;lkljiojoii
pawankamal3
 
PDF
Beautiful python - PyLadies
Alicia Pérez
 
PPTX
Python Workshop
Assem CHELLI
 
PPTX
Python String Revisited.pptx
Chandrapriya Jayabal
 
PPT
Python study material
Satish Nagabhushan
 
PPTX
Python material
Ruchika Sinha
 
PDF
Python strings
Aswini Dharmaraj
 
PPT
Introduction to Python Programming.ppt
UmeshKumarSingh31
 
Strings3a4esrxdfgcbhjjjjjiiol;lkljiojoii
pawankamal3
 
Beautiful python - PyLadies
Alicia Pérez
 
Python Workshop
Assem CHELLI
 
Python String Revisited.pptx
Chandrapriya Jayabal
 
Python study material
Satish Nagabhushan
 
Python material
Ruchika Sinha
 
Python strings
Aswini Dharmaraj
 
Introduction to Python Programming.ppt
UmeshKumarSingh31
 

Similar to Python Programming-UNIT-II - Strings.pptx (20)

PPT
Python
Vishal Sancheti
 
PPTX
Basics of Python
Ease3
 
PPT
Python course in_mumbai
vibrantuser
 
PPT
Python course in_mumbai
vibrantuser
 
PPTX
Python Lecture 6
Inzamam Baig
 
PPTX
Pythonlearn-08-Lists.pptx
MihirDatir
 
PPTX
Chapter 11 Strings.pptx
afsheenfaiq2
 
PPTX
Chapter 11 Strings and methods [Autosaved].pptx
geethar79
 
PPTX
Python Datatypes by SujithKumar
Sujith Kumar
 
PPTX
Strings.pptxbfffffffffffffffffffffffffffffffffffffffffd
pawankamal3
 
PDF
Numpy_Cheat_Sheet.pdf
SkyNerve
 
ODP
Python Day1
Mantavya Gajjar
 
PDF
Py4Inf-08-Lists ListsListsListsListsListsListsListsListsListsListsListsLists.pdf
horiamommand
 
PDF
Python elements list you can study .pdf
AUNGHTET61
 
PPTX
python chapter 1
Raghu nath
 
PPTX
Python chapter 2
Raghu nath
 
PPTX
Python Session - 3
AnirudhaGaikwad4
 
PDF
Learn 90% of Python in 90 Minutes
Matt Harrison
 
PPTX
Chapter 14 strings
Praveen M Jigajinni
 
Basics of Python
Ease3
 
Python course in_mumbai
vibrantuser
 
Python course in_mumbai
vibrantuser
 
Python Lecture 6
Inzamam Baig
 
Pythonlearn-08-Lists.pptx
MihirDatir
 
Chapter 11 Strings.pptx
afsheenfaiq2
 
Chapter 11 Strings and methods [Autosaved].pptx
geethar79
 
Python Datatypes by SujithKumar
Sujith Kumar
 
Strings.pptxbfffffffffffffffffffffffffffffffffffffffffd
pawankamal3
 
Numpy_Cheat_Sheet.pdf
SkyNerve
 
Python Day1
Mantavya Gajjar
 
Py4Inf-08-Lists ListsListsListsListsListsListsListsListsListsListsListsLists.pdf
horiamommand
 
Python elements list you can study .pdf
AUNGHTET61
 
python chapter 1
Raghu nath
 
Python chapter 2
Raghu nath
 
Python Session - 3
AnirudhaGaikwad4
 
Learn 90% of Python in 90 Minutes
Matt Harrison
 
Chapter 14 strings
Praveen M Jigajinni
 
Ad

Recently uploaded (20)

PDF
Electricity-Magnetic-and-Heating-Effects 4th Chapter/8th-science-curiosity.pd...
Sandeep Swamy
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
PPTX
Strengthening open access through collaboration: building connections with OP...
Jisc
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PPT
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PPTX
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
PPTX
How to Manage Global Discount in Odoo 18 POS
Celine George
 
PDF
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PPTX
Congenital Hypothyroidism pptx
AneetaSharma15
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PDF
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PPTX
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
 
PDF
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
Sourav Kr Podder
 
Electricity-Magnetic-and-Heating-Effects 4th Chapter/8th-science-curiosity.pd...
Sandeep Swamy
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
Strengthening open access through collaboration: building connections with OP...
Jisc
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
How to Manage Global Discount in Odoo 18 POS
Celine George
 
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
Congenital Hypothyroidism pptx
AneetaSharma15
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
 
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
Open Quiz Monsoon Mind Game Prelims.pptx
Sourav Kr Podder
 
Ad

Python Programming-UNIT-II - Strings.pptx

  • 1. PYTHON PROGRAMMING UNIT II - STRINGS Dr. D.Kavitha
  • 2. STRINGS  A string consists of a sequence of characters, which includes letters, numbers, punctuation marks and spaces.  To represent strings, you can use a single quote, double quotes or triple quotes.  Creating and Storing Strings  single_quote = 'This is a single message'  2. >>> double_quote = "Hey it is my book"  3. >>> single_char_string = "A"  4. >>> empty_string = ""  5. >>> empty_string = ''  6. >>> single_within_double_quote = "Opportunities don't happen. You create them.“  8. >>> same_quotes = 'I've an idea'  9. >>> triple_quote_string = '''This  … is  … triple  … quote'''
  • 3. STRINGS  A single character is also treated as string.  A string does not need to have any characters in it - empty strings.  A string enclosed in double quotation marks can contain single quotation marks  if one type of quotation mark surrounds the string, you have to use the other type within it.  If you want to include the same quotation marks within a string as you have used to enclose the string, then you need to preface the inner quote with a backslash.  If you have a string spanning multiple lines, then it can be included within triple quotes.  All white spaces and newlines used inside the triple quotes are literally reflected in the string  You can find the type of a variable by passing it as an argument to type() function. Python strings are of str data type .
  • 4. STRINGS  The str() Function  The str() function returns a string which is considered an informal or nicely printable  representation of the given object. The syntax for str() function is, str(object)  It returns a string version of the object. If the object is not provided, then it returns an  empty string.  1. >>> str(10)  '10'  2. >>> create_string = str()  3. >>> type(create_string)  <class 'str'>  Here integer type is converted to string type. Notice the single quotes to represent the  string . The ➀ create_string is an empty string of type ➁ str . ➂
  • 5. Basic String Operations  + - operator is used to concatenate strings. Cannot use the + operator to concatenate values of two different types  1. >>> string_1 = "face"  2. >>> string_2 = "book"  3. >>> concatenated_string = string_1 + string_2  4. >>> concatenated_string  'facebook‘  singer = 50 + "cent"  Traceback (most recent call last):  File "<stdin>", line 1, in <module>  TypeError: unsupported operand type(s) for +: 'int' and 'str'  8. >>> singer = str(50) + "cent"  9. >>> singer  '50cent'
  • 6. Basic String Operations  * - operator is used to create a repeated sequence of strings.  >>> rep_of_string = "wow" * 5  >>> rep_of_string  ‘wowwowwowwowwow’  presence of a string in another string using in and not in membership operators.  1. >>> fruit_string = "apple is a fruit"  2. >>> fruit_sub_string = "apple"  3. >>> fruit_sub_string in fruit_string  True  A substring is any sequence of characters that is contained in a string.
  • 7. String Comparison  Strings can be compared using various comparison operators (>, <, <=, >=, ==, !=)  Python compares strings using ASCII value of the characters.  1. >>> "january" == "jane"  False  2. >>> "january" != "jane"  True  3. >>> "january" < "jane"  False  4. >>> "january" > "jane"  True  5. >>> "filled" > ""  True
  • 8. Built-In Functions Used on Strings  Sample Built-In Functions and Description  len() - The len() function calculates the number of characters in a string. The white space characters are also counted.  max() - The max() function returns a character having highest ASCII value in given string.  min() - The min() function returns character having lowest ASCII value.  1. >>> count_characters = len("eskimos")  2. >>> count_characters  7  3. >>> max("axel")  'x'  4. >>> min("brad")  'a'
  • 9. Accessing Characters in String by Index Number  Each character in the string occupies a position in the string.  The first character is at index 0; the next character is at index 1, and so on.  The length of a string is the number of characters in it.  You can access each character in a string using a subscript operator i.e., a square bracket.  Square brackets are used to perform indexing in a string to get the value at a specific index or position. This is also called subscript operator.  string_name[index]  index is usually in the range of 0 to len-1 or - from the end of the string starting from an index number of −1(negative indexing)  1. >>> word_phrase = "be yourself”  2. >>> word_phrase[3] >>> word_phrase[-1]  ‘y’ 'f'
  • 10. Accessing Characters in String by Index Number
  • 11. String Slicing and Joining  A substring is created when slicing the strings . "slice" syntax : string_name[start:end[:step]]  1. >>> healthy_drink = "green tea"  2. >>> healthy_drink[0:3] >>> healthy_drink[-3:-1]  'gre‘ 'te'  3. >>> healthy_drink[:5] >>> healthy_drink[6:-1]  'green‘ 'te'  4. >>> healthy_drink[6:] >>> healthy_drink[::3]  'tea‘ ‘get’  5. >>> healthy_drink[:]  'green tea'  6. >>> healthy_drink[4:4]  ‘’ empty string  7. >>> healthy_drink[6:20]  'tea'
  • 12. String Slicing  Write Python Code to Determine Whether the Given String Is a Palindrome or Not Using Slicing  def main():  user_string = input("Enter string: ")  if user_string == user_string[::-1]:  print(f"User entered string is palindrome")  else:  print(f"User entered string is not a palindrome")  if __name__ == "__main__":  main()  Output  Case 1:  Enter string: madam  User entered string is palindrome
  • 13. Joining Strings Using join() Method  join() - concatenate strings.  Syntax: string_name.join(sequence)  sequence can be string or list.  If the sequence is a string, then join() function inserts string_name between each character of the string sequence and returns the concatenated string.  If the sequence is a list, then join() function inserts string_name between each item of list sequence and returns the concatenated string.  It should be noted that all the items in the list should be of string type.
  • 14. Joining Strings Using join() Method 1. >>> date_of_birth = ["17", "09", "1950"] 2. >>> ":".join(date_of_birth) '17:09:1950' 3. >>> social_app = ["instagram", "is", "an", "photo", "sharing", "application"] 4. >>> " ".join(social_app) 'instagram is an photo sharing application' 5. >>> numbers = "123" 6. >>> characters = "amy" 7. >>> password = numbers.join(characters) 8. >>> password 'a123m123y'
  • 15. Split Strings Using split() Method  The split() method returns a list of string items by breaking up the string using the delimiter string. The syntax of split() method is string_name.split([separator [, maxsplit]])  Separator - based on the specified separator, given string is split into list of strings,  Maxsplit - at most maxsplit splits are done, no limit – not specified or −1  1. >>> inventors = "edison, tesla, marconi, newton"  2. >>> inventors.split(",")  ['edison', ' tesla', ' marconi', ' newton']  3. >>> watches = "rolex hublot cartier omega"  4. >>> watches.split()  ['rolex', 'hublot', 'cartier', 'omega']
  • 16. Strings Are Immutable  strings are immutable, it cannot be modified  can assign different string values to the same string variable.  1. >>> stre = "dollar"  2. >>> stre[0] = "c"  Traceback (most recent call last):  File "<stdin>", line 1, in <module>  TypeError: 'str' object does not support item assignment  3. >>> str1 = "c" + stre[1:]  4. >>> str1  'collar'  5. >>> stre = "rollar"  6. >>> stre  'rollar'
  • 17. String Traversing  Characters of a string can be traversed using the for loop.  Write Python Program to Calculate the Length of a String Without Using Built-In len() Function  def main():  user_string = input("Enter a string: ")  length = 0  for chr in user_string:  length += 1  print(f"The length of user entered string is {length} ")  if __name__ == "__main__":  main()
  • 18. String Methods  1. >>> dir(str)  ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__',  '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__',  '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__  mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',  '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize',  'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format',  'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower',  'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip',  'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip',  'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']  Various methods associated with str are displayed
  • 19. Formatting Strings  %-formatting  str.format()  f-strings - formatted strings  %-formatting - int, str and doubles can be formatted. Pass only single value.  %s - String (or any object with a string representation, like numbers)  %d - Integers  %f - Floating point numbers  %.<number of digits>f - Floating point numbers with a fixed amount of digits to the right of the dot.  %x/%X - Integers in hex representation (lowercase/uppercase)  >>>import math  >>> print('The value of pi is approximately %5.3f.' % math.pi)  >>> The value of pi is approximately 3.142.
  • 20. Str.format()  The syntax for format() method is,  str.format(p0, p1, ..., k0=v0, k1=v1, ...) where p0, p1,... are called as positional arguments and, k0, k1,... are keyword arguments with their assigned values of v0, v1,... respectively.  Positional arguments are a list of arguments that can be accessed with an index of argument inside curly braces like {index}. Index value starts from zero.  Keyword arguments are a list of arguments of type keyword = value, that can be accessed with the name of the argument inside curly braces like {keyword}.  Here, str is a mixture of text and curly braces of indexed or keyword types
  • 21. Str.format()  1. country = input("Which country do you live in?")  2. print("I live in {0}".format(country))  Output  Which country do you live in? India  I live in India  1. a = 10  2. b = 20  3. print("The values of a is {0} and b is {1}".format(a, b))  4. print("The values of b is {1} and a is {0}".format(a, b))  Output  The values of a is 10 and b is 20  The values of b is 20 and a is 10  1. >>> print("Give me {ball} ball".format(ball = "tennis"))  Give me tennis ball
  • 22. F’string  f'string_statements {variable_name [: {width}.{precision}]}’  print(f'{name:10} ==> {phone:10d}') ...  >>> f'result: {value:{width}.{precision}}'
  • 23. Escape Sequences  Escape Sequences are a combination of a backslash () followed by either a letter or a combination of letters and digits. Escape sequences are also called as control sequences.  The backslash () character is used to escape the meaning of characters that follow it by substituting their special meaning with an alternate interpretation.
  • 24. Raw Strings  A raw string is created by prefixing the character r to the string. In Python, a raw string ignores all types of formatting within a string including the escape characters.  1. >>> print(r"Bible Says, "Taste and see that the LORD is good; blessed is the man who takes refuge in him."")  Bible Says, "Taste and see that the LORD is good; blessed is the man who takes refuge in him.  Unicodes  Unicode Standard provides a unique number for every character, no matter what platform, device application or language. Ex: UTF-8, UTF-16, and UTF-32  Regular Python strings are not Unicode: they are just plain bytes. To create a Unicode  string, use the 'u' prefix on the string literal. For example,  1. >>> unicode_string = u'A unicode u018e string xf1'  2. >>> unicode_string  'A unicode string ñ'