📖 generic · 12th TN - English Medium · COMPUTER SCIENCE · Page 120definition

STRINGS AND STRING MANIPULATION · Part 4

Chapter 3: 4 · COMPUTER SCIENCE

Example

Example II : slice a substring from index to >>> print (str1[ : ]) THIRU Example III : slice a substring using index to but without specifying the beginning index. >>> print (str1[: ]) THIRU Example IV : slice a substring using the start index alone without specifying the end index. >>> print (str1[ :]) KURAL 12th Computer Chapter - - str1="COMPUTER" index= for i in str1: print (str1[:index+ ]) index+= Output C C O C O M C O M P C O M P U C O M P U T C O M P U T E C O M P U T E R Example V : Program to slice substrings using for loop (v) Stride when slicing string When the slicing operation, you can specify a third argument as the stride, which refers to the number of characters to move forward after the first character is retrieved from the string. The default value of stride is . >>> str1 = "Welcome to learn Python" >>> print (str1[ ]) learn >>> print (str1[ : ]) r >>> print (str1[ : ]) er >>> print (str1[:: ]) Wceoenyo Example Note: Remember that, python takes the last value as n- You can also use negative value as stride (third argument). If you specify a negative value, it prints in reverse order. 12th Computer Chapter - - Strings and String Manipulation >>> str1 = "Welcome to learn Python" >>> print(str1[::- ]) nhy re teolW Example . String Formatting Operators The string formatting operator is one of the most exciting feature of python. The formatting operator % is used to construct strings, replacing parts of the strings with the data stored in variables. Syntax: (“String to be display with %val1 and %val2” %(val1, val2)) name = "Rajarajan" mark = print ("Name: %s and Marks: %d" %(name,mark)) Output Name: Rajarajan and Marks: Example . Formatting characters Format characters USAGE %c Character %d (or) %i Signed decimal integer %s String %u Unsigned decimal integer %o Octal integer %x or %X Hexadecimal integer (lower case x refers a-f; upper case X

Related topics

Have a question about this topic?

Get an AI answer grounded in your actual textbook — with the exact page reference.

Ask AI about this topic →