Python String
15 AprilConvert Hex to String in Python
1. Introduction The representation of data in Hexadecimal is commonly used in computer programming. It is used to represent binary data in a human-readable form, as understanding the machine language (i.e., Binary) is difficult for humans. Hexadecimal notation is used in networking protocols, e.g., IPv6 and cryptography, and widely used in graphics, e.g. to represent […]
25 DecemberEncode String to UTF-8 in Python
The UTF-8 encoding is used by default in Python and represents 8-bit Unicode values. The upgrade to Python 3 saw a major change in using ASCII characters to Unicode characters by default for strings. Encode String to UTF-8 in Python This tutorial will demonstrate how to encode string to UTF-8 in Python. Using the encode() […]
23 DecemberHow To Do Line Continuation in Python
Using Backslash (\) Operator We can use \ operator for line continuation in string and number expression as follows. Line Continuation in String To do line continuation in Python Strings: Use the backslash operator to do line continuation for writing strings in Python. #use backslash in a string with single quotation marks my_string1 = 'This […]
16 DecemberConvert List to Comma Separated String in Python
Use .join() Method To convert a list to a comma separated string in Python, use the .join() method. join() method takes all elements in an iterable and joins them into one string with delimiter as separator. list_of_strings = ['string_one', 'string_two', 'string_three'] comma_separated_strings = ','.join(list_of_strings) print(comma_separated_strings) string_one,string_two,string_three First, we created a list having string-type elements and […]
09 DecemberCount Occurrences of Character in String in Python
Using count() Method Use String’s count() method to count occurrences of character in String in Python e.g. my_string.count(character). count() method returns number of occurrences of the character in String. my_string = "This code snippet counts occurrences of a character in a string in python" character = 'c' count = my_string.count(character) print(f'Occurrences of character \'{character}\': {count}') […]
08 DecemberFind Character in String in Python
Using find() Method To find the character in a string in Python: Use the find() method to find the index of the first occurrence of the supplied character in the input String. Use an if statement to check if the returned index is not -1; if so, print that index; otherwise, print an error. my_string […]
05 DecemberRemove Substring from String in Python
Use replace() Method To eliminate a substring from string in Python: Define and initiate my_string. Declare substring to remove it from my_string. Use replace() method to remove substring from string. e.g. my_string = my_string.replace(substring, "") my_string = "The code $#jc@ snippet removes a substring $#jc@ from a string in python." substring = "$#jc@ " print(f'My […]
29 NovemberPrefix b Before String in Python
Prefix b Before String in Python Prefix b before String denotes a byte String. By putting b before String, you can convert String to bytes in Python. The upgrade from Python 2 to Python 3 was considered a major change as many new features were introduced and quite a few changes were done to the […]
28 NovemberGet String Between Two Characters in Python
Using the string-slicing technique To get String between two characters in Python: Use String’s find() method to find indices of both the characters. Use String’s slicing to get String between indices of the two characters. def getSubstringBetweenTwoChars(ch1,ch2,str): return s[s.find(ch1)+1:s.find(ch2)] s = 'Java2Blog' s2= getSubstringBetweenTwoChars('J','g',s) print(s2) Output: ava2Blo In the above example, we found the position […]
- 1
- 2
- 3
- …
- 6
- Nextkeyboard_arrow_right