#!/usr/bin/python3#stringvar1='Hello world!'var2="ABCDEFG"Print("var1:"+var1)Print("var2:"+var2)Print("Var1[0]:"+var1[0])Print("var2[2:]:"+var2[2:])Print("Var2[2:3]:"+var2[2:3])Print("var1:"+var1*2)#Transfer character"""\ \ backslash symbol \ ' single quotation mark \a bell \b backspace \ n newline \ tab \ r Enter"""#python character operator"""+ String Connection * Duplicate output string [] Index gets part of the string [:] intercepts a part of a string in characters if the given character is not in% formatted string"""#python formatting symbols#string Formatting"""%s formatted string%c formatted character%d formatted integer%f formatted floating-point number, can be specified in bits%r universal format character, direct output"""Pi=3.1415926str1="Good Boy"Print("pi=%.2f"%pi)Print("str1%s"%str1)#built-in functionsStr2="abcdefGHabcd123456789"#S.count (sub[, start[, end]), intPrint("String%s"%str2)Print("string Length:", Len (str2))Print("returns the number of occurrences count (def):%d"% (Str2.count ("ABCD")))#S.endswith (suffix[, start[, end]), BOOLPrint("whether to end with a character endswith (def):%r"% (Str2.endswith ("ABCD")))#find not found back-1#S.index (sub[, start[, end]), int and find, but no error is foundPrint("Find", Str2.find ("BCD", 0,len (str2)))#rfind not found. Return-1Print("RFind", Str2.rfind ("BCD", 0,len (str2)))#s.isalnum (), bool at least one character and all consist of characters and numbers (special symbols return false, such as.)Print("whether it is all by numbers and characters Str2.isalnum ()", Str2.isalnum ())#S.isalpha (), BOOL has at least one character and is all charactersPrint("are all composed of characters Str2.isalpha ()", Str2.isalpha ())#s.islower (), bool with a letter and all lowercase to return truePrint("whether the characters are all lowercase str2.islower ()", Str2.islower ())#s.isupper (), bool with a letter and all uppercase to return TruePrint("whether the characters are all uppercase Str2.isupper ()", Str2.isupper ())##S. IsDigit () bool is all made up of numbersPrint("is all made up of numbers str2.isdigit ()", Str2.isdigit ())Print("is all made up of numbers str2.isnumeric ()", Str2.isnumeric ())Print("is only a space str2.isspace ()", Str2.isspace ())#Lower () Upper ( )Print("Convert all to lowercase str2.lower ()", Str2.lower ())Print("convert all to uppercase Str2.upper ()", Str2.upper ())Print("Turn uppercase and lowercase characters into uppercase", Str2.swapcase ())
Python: String