Page 153 - CodePilot V5.0 C8
P. 153

String concatenation operator (+): String concatenation operator joins two or more strings
                    into one string.

                  Program 11 A program to demonstrate string concatenation.


                     Program11.py                                                      Output

                  File  Edit  Format   Run    Options   Window    Help              HelloWorld

                  str1 = 'Hello'
                  str2 = 'World'
                  print(str1 + str2)




                     String replication operator (*): The replication operator is used to repeat the string for a given
                    number of times.

                  Program 12 A program to demonstrate string repetition.


                     Program12.py                                                Output

                  File  Edit  Format   Run    Options   Window    Help        HelloHello
                                                                              HelloHelloHelloHelloHello
                  str1 = 'Hello'
                  print(str1 * 2)
                  print(str1 * 5)





                 BUILT-IN STRING FUNCTIONS

                 Python offers a wide range of built-in functions for manipulating strings. Some built-in functions
                 are shown in the following table:

                    Functions                             Description                                 Syntax

                  count()         It returns the number of times a specified value occurs in  string.count(value)
                                  a string.
                  len()           It returns the total number of characters in the string.      len(string)

                  lower()         It converts a string into lower case.                         string.lower()
                  replace()       It returns a string where a specified value is replaced with  string.replace(old_
                                  a specified value.                                            value, new_value)

                  title()         It converts the first character of each word to uppercase.  string.title()

                  upper()         It converts a string into uppercase.                          string.upper()
                  capitalize()    It converts the first character of the string to uppercase.   string.capitalize()







                                                                                                                  151
                                                                                            Step Ahead with Python
   148   149   150   151   152   153   154   155   156   157   158