Questions tagged [string-formatting]
Commonly refers to a number of methods to display an arbitrary number of varied data types into a string.
4,292
questions
2
votes
1
answer
86
views
How to use the new CompositeFormat class from .NET 8.0?
I'm testing migrating a .NET 6.0 application to .NET 8.0, and the following piece of code now throws CA1863 "Use 'CompositeFormat'". But I don't know how.
private const string ...
1
vote
2
answers
97
views
Is a formatted string still a string? Is it still printable, or it is just a bunch of rules?
I am looking at other code on a Java practice. Here is his code:
import java.util.Arrays;
import java.util.stream.Collectors;
public class StripComments {
public static String stripComments(...
1
vote
1
answer
32
views
Lua formatting returns unexpected results
I recently wrote a small program where I'm trying to convert a number of seconds into a more human readable format, but I'm having problems when trying to format the output and somehow I end up ...
-1
votes
1
answer
50
views
Learning python Python TypeError: not enough arguments for format string [duplicate]
I am new to python and trying to learn the basic
just learned how to format string today and couldn't figure out what is wrong with this line of codes
data = ["Zac", "poor fella", ...
0
votes
1
answer
58
views
printf not printing a variable in ubuntu (C)
I have a problem with printing string from a variable using 'printf' and '%s' in c.
when I am compiling the code below and running the exe file in windows using the '<' oparand to enter values from ...
1
vote
1
answer
58
views
The Number() function adds 1 to the numeric result by converting a string to a number [duplicate]
I wanted to convert the string '9333852702227987' into a number with the help of the Number() function when I realized that the output was plus one.
I tried using other methods to convert the string ...
1
vote
1
answer
46
views
Date formatting the month abbreviation behaves differently between .NET Framework 4.8 and .NET 8
This code behaves differently between .NET Framework 4.8 and .NET 5, 6, 7, 8
Console.WriteLine("CurrentCulture is {0}.", CultureInfo.CurrentCulture.Name);
Console.WriteLine("...
0
votes
1
answer
33
views
Go vet Sprinf checking number of arguments
I’ve found an interesting behavior of go vet with Sprintf which I would not expect (it’s
inside the lint check).
Could you explain me why go vet is not throwing a violation in the second numbered case ...
2
votes
3
answers
174
views
How to format heatmap annotation floats to only show the decimal component
I have floats and I'm interested in just the decimal places of them because the integer part is not important (and always will be the same in my problem).
I have to use string formatting because the ...
-2
votes
1
answer
95
views
Unable to get stat of existing directory in Ansible due to variable formatting issue
I'm trying to get the owner of the first directory passed to Ansible variable source_files.
We do not have control over the content of Ansible variable source_files as it is constructed through some ...
0
votes
1
answer
82
views
String interpolation, string formatting and null coalescing operator at once
I need this:
If the following formatted value is null, display N/A. Otherwise, display the formatted value. It needs to be with string interpolation. I tried to do something like nested interpolation:
...
0
votes
1
answer
75
views
In Python, trying to remove last comma in print statement output [duplicate]
I am trying to come up with a way to remove the last comma in the output of this print statement. The trick is trying to get everything to come out on a single output line while removing the trailing ...
0
votes
1
answer
64
views
Dynamic padding with period using f-string
Given a width n, and an index i, how to generate a string of length n with x at index i, and . at the remaining indices?
For example:
Input: n = 4, i = 1
Output: ".x.."
Currently I'm doing:
...
0
votes
1
answer
82
views
T-SQL statement to Print full table with aligned data
For context, I'm trying to create an email where users can see the queried data as a table. I want the data (from each column) to align at the same position for each row (hence, a table). The data in ...
0
votes
0
answers
32
views
String format putting leading 0 on the wrong side [duplicate]
So I'm trying to make a function that converts middle-endian date to ISO 8601.
The "convert" code works as I want it to work but my String Format isnt printing how im wanting it to print.
...
0
votes
4
answers
183
views
Formatting a string to insert a decimal point
I have an integer 4 digits wide, which I convert to a string using sprintf.
This string array is supposed to represent a decimal number XX.XX.
eg.
uint16_t Value;
char buff [10];
Value = 1234;
sprintf(...
0
votes
1
answer
95
views
Maui StringFormat in label, possible to change FontAttribute inline?
Really basic question, but I can't find the answer:
Is it possible to change the font attribute of a string in a label that's been formatted?
Specifically, I have this Label:
<Label Style="{...
2
votes
2
answers
253
views
How to do string interpolation (formatting) with Mojo?
I couldn't find how to do string formatting in Mojo in the docs.
I have tried the common Python methods (str.format(), f-strings, % placeholder), but nothing worked out so far. I know Mojo is still a ...
1
vote
2
answers
49
views
Aligning strings in the terminal with c#
I am looking for a little assistance. I am new to C# and am trying to build a simple addition/ subtraction game for my kid to practice his skills. I have the basics working (more to develop on it) so ...
-1
votes
1
answer
22
views
What syntax could i use to padd spaces with different characters in this example?
I have found this piece of code from another user on CodeWars after resolving some exercise, where he used format function.
def rgb(r, g, b):
round = lambda x: min(255, max(x, 0))
return (&...
0
votes
1
answer
98
views
How do I set alignment and restrict character length in an f string? [duplicate]
I am trying to output from a CSV reader a table with three columns. Each column is formatted differently. The first column has a maximum and minimum of 44 characters.
This is the print statement:
...
-1
votes
1
answer
35
views
I can't use variables in a long title in Matplotlib if I split it to fit the PEP8 length [duplicate]
I'm trying to use a title with variables to show some data which indicates some parameters used for generating the plot, and the date and hour to know when it was generated.
The problem is it only ...
-1
votes
2
answers
47
views
How to put multiple numbers into string regardless of the number for String.format
I want to create a sentence inserting some numbers like "Your tag numbers are 1, 2, 3"
The number of the numbers varies depending on the input like (1,2,3) or (1,2,3,4). How can it be ...
1
vote
1
answer
34
views
How to deal optionnal hour format in pandas to_datetime?
I need to deal with a Dataframe of string (a run course btw) where people finish before one hour so it is formated "%M:%S" eg: "54:45" and some people finish after 1 hour, so the ...
1
vote
1
answer
74
views
What format is this, and how do I convert a byte array into this string format?
I need to edit (change) the image stored in a .resx file that is attached to a Winforms form (actually, it is being loaded into a textbox on an Active Reports (AR) report designer).
The current image ...
0
votes
1
answer
50
views
How to format msmtp terminal command string for delphi or lazarus?
I am testing MSMTP terminal command for sending emails.
$ echo -e "Subject: Testing msmtp \nHello, this is msmtp." | msmtp [email protected]
This terminal command works flawlessly just as I ...
0
votes
0
answers
57
views
How can I format datetimes to strings containing asterisks/wildcards in Python?
I'd like to be able to do something like datetime(2022, 1, 1, 12, "*", "*", "*").strftime("%Y%m%d_%H%M%S") and have it output "20220101_***". How can ...
0
votes
3
answers
54
views
Split() at a meta character * with pyspark and Java
I've fighting with this code to split the column name_surname in 2 differents columns. The content of the column name_surname is stored like i.e.: David*Green so the purpose of these query is to ...
0
votes
2
answers
60
views
show decimal places of number in WPF according to user selection
I want to display a number of type double in a WPF window. The user should be enabled to select how many decimal places should be displayed.
I want to solve this in the view (XAML) directly without ...
0
votes
1
answer
110
views
How can I define a string format for a double that displays the value as if it was divided?
Suppose I have a double = 99.00;. How can I define a string format that will output me a string like "0.099" when using double.ToString(format);. As you can see I want to format my double ...
1
vote
1
answer
42
views
Transform long written out date to standard formatting in Python?
I have a series of text blocks that contain a date written as "The first Wednesday of September, 2021" or "The third Monday in July, 2022", etc. I am not sure of the best way to ...
4
votes
4
answers
114
views
I want to add a string to another string with {} around that string
From the title it sounds very confusing but this is what I'm trying to say.
We have a word, and I want to add it to mystring = "" but I want to have {} around the word (mind you I cant hard ...
1
vote
1
answer
63
views
adding a comma to float format in python for stack bar charts
I'm trying to print out this stacked bar chart and I want to add a comma after numbers in thousands. For example, if 7000 -> 7,000; if 20000 -> 20,000.
I'm trying to add the code fmt='{:,.0f} to ...
0
votes
0
answers
72
views
Use String.Format() to align variable strings to the left and right of the fixed length string with overlap processing
I'm trying to use format string for complex formatting with truncation.
Using format strings (stored in config file) would allow me to define different ways of truncating text without need to create ...
3
votes
2
answers
149
views
Quote string value in F-string in Python
I'm trying to quote one of the values I send to an f-string in Python:
f'This is the value I want quoted: \'{value}\''
This works, but I wonder if there's a formatting option that does this for me, ...
-1
votes
2
answers
54
views
Python parsing string -> binary tree
I want to create a binary tree by parsing a string like "27(3(4,5),2(,))". Where 27 is the root key and 3 is its left child and 2 is its right child.... I want to split at the commas and ...
-2
votes
1
answer
113
views
illegal escape character from String.format()
Edit:
I am getting "error: illegal escape character" when I try to compile my Java program.
Minimal reproducible example:
public static void main(String... args) {
String output = String....
0
votes
1
answer
25
views
FormatException when custom button is pressed the second time
In unity 2d c# I created a custom button, and when pressed the first time it works perfectly, but when the button is pressed a second time I received an error "FormatException: Input string was ...
0
votes
0
answers
65
views
Python string formatting for precision with no trailing zeros
I have some floating-point numbers containing zero or more decimal places. I want to display them with no more than two decimal places, with no trailing zeros. In other words, I'm looking for a ...
0
votes
1
answer
69
views
In Python, is there a max length float to string formatting parameter?
I have a program that prints matrices in the console which means I want the spacing for each matrix entry to be the same, regardless of how big the number is, positive or negative. I want numbers that ...
0
votes
2
answers
159
views
Is String formatting more expensive than casting a Double to an Int?
I have to choose between one of the two in a program I'm writing in Swift but I guess it doesn't really matter what programming language I'm using.
I know String formatting is very expensive and ...
1
vote
0
answers
47
views
How to tell if a string could have been generated by formatting another string? [duplicate]
Say we have:
f = "The object named '{}' is corrupted."
s = "The object named 'my_object' is corrupted."
s is equal to f.format("my_object"). If we didn't know that, how ...
0
votes
4
answers
72
views
Reorganize txt file - Identify date string, relocate and insert string
I have got an input txt file formatted like this:
27/04/2023
00:00 0.1
06:00 0.5
23:00 0.9
28/04/2023
00:00 0.1
06:00 0.5
23:00 0.9
29/04/2023
00:00 0.1
06:00 0.5
23:00 0.9
The output should look ...
0
votes
0
answers
34
views
How to get object types to print in raised errors in python?
I'm trying to raise an error that contains the type of some object, like so:
x = 5
raise TypeError(f"Input was type: {type(x)}")
However this prints:
----------------------------------------...
0
votes
0
answers
82
views
AWS Quicksight render long string as pretty printed json
In an AWS Quicksight dashboard I have a need to display JSON in human readable pretty printed format. I have so far been able to display the JSON as a string but since there are no line breaks it is ...
0
votes
1
answer
157
views
Google Form + Apps Script: setting HelpText and alignment --> justify
I created a Google form starting from Google Sheets and App Scripts.
I' am struggling to find a way to justify long strings when using a setHelpText for a Section Header Item:
var sectionL = form....
0
votes
0
answers
33
views
StringFormat does not work in code-behind binding(WPF) [duplicate]
Here is the xaml part.
<StackPanel Orientation="Vertical">
<Label x:Name="TestLabel"/>
<TextBox x:Name="TestTextBox"
Text=&...
0
votes
2
answers
68
views
How to display string formatters as plain text in Java?
I have a string in Java that contains special characters like \n and \t which are interpreted as control characters and produce new lines and tabs when displayed. I want to display these special ...
2
votes
2
answers
49
views
Sorting data between 2 lists into a new list, and formatting a list of strings using the data saved in the new list
apologies if this isn't very clear, this is my first time asking a question here so I hope I can explain my problem correctly.
I have the following lists with different values:
A_list = ['A', 'A', 'B',...
-2
votes
1
answer
279
views
String Format an integer to look like a social security number
After the users input of a social security number I would like to take the integer that is entered and display is as a social security number. So that XXXXXXXXX becomes XXX-XX-XXXX.
public String ...