You can easily write one:
padding_char = '*'
def setLength(s, max_length):
return (s + padding_char * max_length) [:max_length]
exampleA=setLength('I love it dude!',15)
print(exampleA)
exampleB=setLength('I love it!',15)
print(exampleB)
exampleC=setLength('I love it dude! How can we add more to this string?',15)
print(exampleC)
prints :
I love it dude!
I love it!*****
I love it dude!
Answer from Gelineau on Stack OverflowW3Schools
w3schools.com › python › gloss_python_string_length.asp
Python String Length
The len() function returns the length of a string: a = "Hello, World!" print(len(a)) Try it Yourself » · Python Strings Tutorial String Literals Assigning a String to a Variable Multiline Strings Strings are Arrays Slicing a String Negative ...
Top answer 1 of 3
1
You can easily write one:
padding_char = '*'
def setLength(s, max_length):
return (s + padding_char * max_length) [:max_length]
exampleA=setLength('I love it dude!',15)
print(exampleA)
exampleB=setLength('I love it!',15)
print(exampleB)
exampleC=setLength('I love it dude! How can we add more to this string?',15)
print(exampleC)
prints :
I love it dude!
I love it!*****
I love it dude!
2 of 3
1
Using str.format() might be an option:
'{0:\u0000<15.15}'.format('I love it dude!')
where \u0000 is the character to fill up to the minimum length, < is the alignment of the text (in this case left), the first 15 the minimum length, the second 15 the maximum length.
Top answer 1 of 10
179
This is super simple with format:
>>> a = "John"
>>> "{:<15}".format(a)
'John '
2 of 10
132
You can use the ljust method on strings.
>>> name = 'John'
>>> name.ljust(15)
'John '
Note that if the name is longer than 15 characters, ljust won't truncate it. If you want to end up with exactly 15 characters, you can slice the resulting string:
>>> name.ljust(15)[:15]
Tutorialspoint
tutorialspoint.com › python › string_len.htm
Python String len() Method
The len() method determines how many characters are there in the string including punctuation, space, and all type of special characters. The number of elements which is stored in the object is never calculated, so this method helps in providing ...
Call: +917738666252
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
GeeksforGeeks
geeksforgeeks.org › python › python-padding-a-string-upto-fixed-length
Python | Padding a string upto fixed length - GeeksforGeeks
March 23, 2023 - # Python code to demonstrate # pad spaces in string # upto fixed length # initialising string ini_string = "123abcjw" padding_size = 15 # printing string and its length print ("initial string : ", ini_string, len(ini_string)) # code to pad spaces in string res = ini_string + " "*(padding_size - len(ini_string)) # printing string and its length print ("final string : ", res, len(res))
GeeksforGeeks
geeksforgeeks.org › python › python-string-length-len
Python string length - GeeksforGeeks
October 15, 2024 - ... # Python program to demonstrate the use of # len() method # with tuple tup = (1,2,3) print(len(tup)) # with list l = [1,2,3,4] print(len(l)) ... In this example, we are using len() with a boolean value and find TypeError as a result.
Dot Net Perls
dotnetperls.com › len-python
Python - len (String Length) - Dot Net Perls
This string has zero characters but is not None. Tip Len relies on the type of the variable passed to it. A NoneType has no len built-in support. # Has length of 3: value = "cat" print(len(value)) # Has length of 0: value = "" print(len(value)) # Causes TypeError: value = None print(len(value))
W3Schools
w3schools.com › python › gloss_python_set_length.asp
Python Get the Length of a Set
Remove List Duplicates Reverse ... Python Interview Q&A Python Training ... To determine how many items a set has, use the len() method....
W3Schools
devcom.w3schools.com › python › gloss_python_string_length.asp
Python String Length
Python Overview Python Built-in ... Python Exercises Python Quiz Python Certificate ... To get the length of a string, use the len() function....
Stanford
web.stanford.edu › class › archive › cs › cs106a › cs106a.1204 › handouts › py-string.html
Python Strings
The str() function serves to convert many values to a string form. Here is an example this code computes the str form of the number 123: ... Looking carefully at the values, 123 is a number, while '123' is a string length-3, made of the three chars '1' '2' and '3'.